VSTO Pluging: Copying format restarts a custom undo record

Kirill Lapshin 0 Reputation points
2024-08-16T16:58:10.2066667+00:00

We are developing a Word add-in using VSTO and have encountered an issue with a custom UndoRecord. The problem arises when we use the CopyFormat method between the StartCustomRecord and EndCustomRecord method calls. Executing CopyFormat causes the existing UndoRecord to close and a new one to start, resulting in an additional record appearing in the undo stack, which is not the desired behaviour.

Here is a simplified version of the method we are using:

csharpCopy code
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.

Our questions are:

  1. Why does CopyFormat cause a new UndoRecord to be created?
  2. How can we prevent this behaviour and ensure that only one undo record is created for the entire operation?

Any insights or suggestions would be greatly appreciated. Thanks in advance!

Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
883 questions
Office
Office
A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.
1,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,989 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
933 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.