Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GOOD CODE: How to save your users from a terrible "undo-mess"

4,239 views
Skip to first unread message

Roemer Lievaart

unread,
Jan 14, 2000, 3:00:00 AM1/14/00
to
Hi,

I'm presently not reading this group anymore but I've got something I
wanted to share, some people may really benefit form this.

You know the problem that after your macro has run, and the user wants
to undo the results of this macro, he has to push ctrl-Z a lot,
instead of just once?

Wouldn't it be nice if there was some code that could undo any amount
of actions your macro did to a document in just ONE "undo"? Well, I
found a difficult way to do it and an easy one. Here's the easy one.

I call it the "UndoSaver" for it saves the user a lot of "undo's".
For instance if you have a macro that does:

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


PLEASE: If you have any improvements, mail them to me
(liev...@dds.nl) too! I do not read this newsgroup anymore due to
lack of time! Thanks!

Regards,

Rmr.

--
-------------------------------------------------------------------------------
Roemer Lievaart |
liev...@dds.nl | Leef bewust:
don't give in to spam | Recycle je geluk.
-------------------------------------------------------------------------------
http://listen.to/roemer http://listen.to/vuorkest

0 new messages