Creating Custom Undo Records in Word 2010
Handy Programming Tips for Microsoft Word 2010: Learn how to increase the effectiveness of your Undo actions in Microsoft Word 2010 documents.
Last modified: May 10, 2011
Applies to: Office 2010 | VBA | Word 2010
Published: May 2011
Provided by: Frank Rice, Microsoft Corporation
Microsoft Word enables you to reverse actions that you take in a document by using the Undo option. You can improve Undo by wrapping two or more actions inside a custom Undo record, which reverses those actions as a single item. In this topic, you programmatically create two custom Undo records and then demonstrate how to use them. To complete this task, you must do the following:
In this task, you open a Word 2010 document, open the Visual Basic Editor, and then insert a standard module.
To add a standard module to a Word document
Start Word 2010.
On the Developer tab, click Visual Basic to open the Visual Basic Editor.
If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.
On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.
In this task, you add programming code that creates two custom Undo records and then adds individual actions to those records. Specifically, it adds some text and a chart to custom Undo records.
To add code to the Visual Basic Editor
In the Projects pane, click Module1.
Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.
Sub UndoRecordTest() Dim ur As UndoRecord ' The Application object supplies a single ' UndoRecord object, which maintains a stack ' of undo actions. You can interact with the stack. Set ur = Application.UndoRecord ur.StartCustomRecord "My Undo Record" ActiveDocument.Range.InsertAfter "Here is some text. " ActiveDocument.Range.InsertAfter "Here is some more text. " ActiveDocument.Range.InsertParagraphAfter ActiveDocument.Range.InsertAfter "After the chart." ActiveDocument.Shapes.AddChart xl3DPie, 100, 100, 200, 200 ur.EndCustomRecord ur.StartCustomRecord "My Second Undo Record" ActiveDocument.Range.InsertAfter "Here is some more text. " ActiveDocument.Range.InsertParagraphAfter Dim paras As Paragraphs Set paras = ActiveDocument.Paragraphs ActiveDocument.Tables.Add paras(paras.Count).Range, 2, 2 ur.EndCustomRecord End Sub
In this task, you add some text to the document to verify that the Undo option is working. Next, you run the code to create two custom Undo records. Then you use the Undo option button on the Quick Access Toolbar to see how these custom records work. Note that undoing any action also undoes any actions that are higher on the stack (that is, any "undo" items that were added later to the Undo stack).
To run the code
In the document, type This is some text.
In the Quick Access Toolbar, click the down arrow next to the Undo button and verify that the text is available to undo.
On the Developers tab, click Macros, select UndoRecordTest, and then click Run.
Close the Visual Basic Editor. In the document window, you should see some additions to the document; specifically, some text and a chart, and then some more text.
On the Quick Access Toolbar, click the arrow next to the Undo button to see the available actions, as shown in Figure 1.
Click My Second Undo Record. Notice that some of the text is removed from the document.
Click the arrow next to the Undo button and then click Typing "This is some text". This removes the text that you typed and My Undo Record, which you added after you typed the text, and therefore was higher in the Undo stack.