Reputation: 6172
I'm developing a Word add-in using VSTO and faced an issue with a custom UndoRecord
. The problem appears when I use the CopyFormat method between the StartCustomRecord
and EndCustomRecord
method calls. Executing CopyFormat
causes the existing UndoRecord to close and start a new one. This results in an additional record appearing in the undo stack, which is not the desired behaviour.
Here is a simplified version of the method I'm using:
private static void PasteRtf(string rtf, Range sourceRange, Range targetRange)
{
var undoRecord = sourceRange.Application.UndoRecord;
try
{
undoRecord.StartCustomRecord("Custom record");
targetRange.InsertParagraphBefore();
targetRange = targetRange.Paragraphs.First.Range;
System.Windows.Clipboard.Clear();
System.Windows.Clipboard.SetData(System.Windows.DataFormats.Rtf, rtf);
targetRange.Paste();
sourceRange.Select();
sourceRange.Application.Selection.CopyFormat();
targetRange.Select();
targetRange.Application.Selection.PasteFormat();
}
finally
{
undoRecord.EndCustomRecord();
}
}
In the above code, StartCustomRecord
is called at the beginning, and EndCustomRecord
is called at the end. However, after executing CopyFormat
, it seems that a new UndoRecord
is created, which results in multiple undo actions for a single operation.
My questions are:
CopyFormat
cause a new UndoRecord
to be created?Any insights or suggestions would be greatly appreciated. Thanks in advance!
Upvotes: 0
Views: 92
Reputation: 128
private static void PasteRtf(string rtf, Range sourceRange, Range targetRange)
{
var undoRecord = sourceRange.Application.UndoRecord;
try
{
undoRecord.StartCustomRecord("Custom record");
targetRange.InsertParagraphBefore();
targetRange = targetRange.Paragraphs.First.Range;
System.Windows.Clipboard.Clear();
System.Windows.Clipboard.SetData(System.Windows.DataFormats.Rtf, rtf);
targetRange.Paste();
targetRange.set_Style(sourceRange.get_Style());
targetRange.Font.Name = sourceRange.Font.Name;
targetRange.Font.Size = sourceRange.Font.Size;
targetRange.Font.Bold = sourceRange.Font.Bold;
targetRange.Font.Italic = sourceRange.Font.Italic;
targetRange.ParagraphFormat.Alignment = sourceRange.ParagraphFormat.Alignment;
}
finally
{
undoRecord.EndCustomRecord();
}
}
Upvotes: 0