I've created a very simple class that should interface with Microsoft Word's UndoRecord in a VSTO addin.
public class ChangesTracker : IDisposable {
private Word.UndoRecord undoRecord;
public ChangesTracker(string name) {
Globals.ThisAddIn.Application.ScreenUpdating = false;
undoRecord = Globals.ThisAddIn.Application.UndoRecord;
undoRecord.StartCustomRecord(name);
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (undoRecord != null) {
undoRecord.EndCustomRecord( );
Globals.ThisAddIn.Application.ScreenUpdating = true;
undoRecord = null;
}
}
}
public void Dispose() {
Dispose(true);
}
~ChangesTracker() {
Dispose(false);
}
}
I use it with a simple using
statement around all the operations that should be grouped
e.g.:
using (var tracker = new ChangesTracker("Something")) {
/// code
}
This seems to be working the majority of the time, except for anything that gets the selection's XML. For example this breaks it.
var xml = Globals.ThisAddIn.Application.Selection.Range.WordOpenXML;
selectionRoot = new XmlDocument( );
selectionRoot.LoadXml(xml);
When inspecting the UndoRecord, the property IsRecordingCustomRecord changes to false and the UndoRecord is ignored from then on. Am I doing something wrong? Is this perhaps yet another bug in Word and if so how do I go around it?