Boatwrenchv8
Programmer
Hello,
I found this macro on a google ( forum. What it does is undo a series of vba actions done by your macro with a single ctrl+z key press (it does not work with the tool bar undo - only the keyboard short cut).
I understand how this macro sets a bookmark, then if the user undoes, the macro undoes all operations until the bookmark is deleted.
What I do not understand is how Sub EditUndo() or Sub EditRedo() is run. I have stepped thru this code and do not understand how they are run to detect that key press.
Please help me understand!
Thanks,
Rich
Sub Test()
StartUndoSaver
' Everything from here on should be undone in just ONE undo
' Just some nonsense code that will produce multiple
' entries in de undo-list
' Of course to be replaced by any code of your own.
Selection.TypeText "Hello"
Selection.TypeParagraph
Selection.Style = wdStyleHeading1
Selection.TypeText "WORLD!"
Selection.TypeParagraph
' Everything until here will be undone in just ONE undo,
' if the user presses ctrl-Z.
EndUndoSaver
End Sub
'This is the code that will do it (Note that the EditUndo and EditRedo
'will capture the Ctrl-Z and Ctrl-Y but it will NOT capture the undo
'and redo-buttons on your commandbar. You'll have to figure that one
'out yourself, but it's not very hard):
Option Explicit
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
If ActiveDocument.Undo = False Then Exit Sub
While BookMarkExists("_InMacro_")
If ActiveDocument.Undo = False Then Exit Sub
Wend
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
I found this macro on a google ( forum. What it does is undo a series of vba actions done by your macro with a single ctrl+z key press (it does not work with the tool bar undo - only the keyboard short cut).
I understand how this macro sets a bookmark, then if the user undoes, the macro undoes all operations until the bookmark is deleted.
What I do not understand is how Sub EditUndo() or Sub EditRedo() is run. I have stepped thru this code and do not understand how they are run to detect that key press.
Please help me understand!
Thanks,
Rich
Sub Test()
StartUndoSaver
' Everything from here on should be undone in just ONE undo
' Just some nonsense code that will produce multiple
' entries in de undo-list
' Of course to be replaced by any code of your own.
Selection.TypeText "Hello"
Selection.TypeParagraph
Selection.Style = wdStyleHeading1
Selection.TypeText "WORLD!"
Selection.TypeParagraph
' Everything until here will be undone in just ONE undo,
' if the user presses ctrl-Z.
EndUndoSaver
End Sub
'This is the code that will do it (Note that the EditUndo and EditRedo
'will capture the Ctrl-Z and Ctrl-Y but it will NOT capture the undo
'and redo-buttons on your commandbar. You'll have to figure that one
'out yourself, but it's not very hard):
Option Explicit
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
If ActiveDocument.Undo = False Then Exit Sub
While BookMarkExists("_InMacro_")
If ActiveDocument.Undo = False Then Exit Sub
Wend
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