6

I have a set of Word macro's that format documents that are sent to from an external source. Usually there are a lot of changes made to the document, all of which are recorded in the undo history, which I don't want.

Is there a way to not add all of these changes to the undo history, but rather just add one undo action that will undo everything the macros have done? Thanks.

David Gard
  • 11,225
  • 36
  • 115
  • 227

2 Answers2

4

This is not available for Word 2007 or earlier. The UndoRecord object was added in Word 2010 and lets you "clump" actions made in VBA into a single UndoRecord, to which a custom "label" for the Undo list can be assigned. The Undo actions can be nested at various levels. More than one UndoRecord object can be created. More information is available in the MSDN documentation

https://msdn.microsoft.com/en-us/library/office/ff821168(v=office.14).aspx

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Is it possible to remove the actions from the stack completely? e.g. I'm using formatting to highlight a section of the document to the user, but this gets undone later by the macro. I don't want the user accidentally undoing the formatting changes, when they expect something else to undo. – Ryan Leach Apr 06 '17 at 03:42
0

The relevant code from @CindyMeister's answer:

Sub MacroWithUndo() 
  Dim objUndo As UndoRecord 
  Set objUndo = Application.UndoRecord 
   
  ' Begin the custom undo record and provide a name for the record 
  objUndo.StartCustomRecord ("My Macro's Undo Description") 
       
  ' Your code here
   
  ' End the custom undo record 
  objUndo.EndCustomRecord 
     
End Sub 

Source:

https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/hh420330(v=office.14)

Christopher Oezbek
  • 23,994
  • 6
  • 61
  • 85