Hi Derek,
There is a way to do this. I don't guarantee that these are perfect
since I've messed around with them a bit. Also, I started to have a
problem whereby when I used Ctrl+Z it would occasionally undo many steps
at once. It's possible that instead of creating macros named EditUndo
and EditRedo, thus changing the built-in commands, it might be better to
give these macros different names from the built-in commands so that
they just have the specialized function of undoing/redoing macros, while
leaving the regular EditUndo/editRedo commands in their default state.
In fact, that's what I've just done myself, and what I would recommend.
Change the macro EditUndo to UndoMacro and change the macro EditRedo to
RedoMacro, and then assign keystrokes to each one, say, Ctrl+Shift+Z to
UndoMacro and Ctrl+Shift+Y to RedoMacro. Then you're not changing the
functionality of the regular Undo/Redo commands and you won't have any
problem with them. Here goes:
1. Put this at the top of your module
Option Explicit
2. Then install these macros.
' NOTE: These macros were provided by Roemer Lievaart
' at (e-mail address removed). It enables Ctrl+Z to undo an
' entire macro in one step. It was on a web page.
Sub StartUndoSaver()
On Error Resume Next
ActiveDocument.Bookmarks.Add "_InMacro_"
On Error GoTo 0
End Sub
Sub EndUndoSaver()
On Error Resume Next
ActiveDocument.Bookmarks("_InMacro_").Delete
On Error GoTo 0
End Sub
Sub EditUndo()
' Catches Ctrl-Z. Loops EditUndo until previous macro is undone.
If ActiveDocument.Undo = False Then Exit Sub
Do While ActiveDocument.Bookmarks.Exists("_InMacro_")
If ActiveDocument.Undo = False Then Exit Sub
Loop
End Sub
Sub EditRedo()
' Catches Ctrl-Y
If ActiveDocument.Redo = False Then Exit Sub
While BookMarkExists("_InMacro_")
If ActiveDocument.Redo = False Then Exit Sub
Wend
End Sub
Private Function BookMarkExists(Name As String) As Boolean
On Error Resume Next
BookMarkExists = Len(ActiveDocument.Bookmarks(Name).Name) > -1
On Error GoTo 0
End Function
3. Then, in any macro that you want to be able to undo in one step, you
do this:
Application.Run "StartUndoSaver"
' your code
Application.Run "EndUndoSaver"