Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word, Macros, Multiple Undos with one keypress - Not understanding

Status
Not open for further replies.

Boatwrenchv8

Programmer
Jul 2, 2007
2
US
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
 
hi,

Have you looked at Tools/customise/keyboard specify a command , macros

Chance,

F, G + B
 
The EditUndo and EditRedo procedures run automatically when the keyboard shortcuts Ctrl+Z and Ctrl+Y are typed, reespectively, because those are the procedure names Word uses internally for those operations. By using the same name in VBA code, your version fires instead of the built-in version. You can trap the File|Save command in the same way (e.g., name a procedure FileSave) and your code will run instead of the native functionality.


Hope this helps.

Regards,
Mike
 
Mike, Thank you for your reply. I did not know that my version would take priority over word's built in version. Now I understand! The bulb is now fully lit.

Rich
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top