(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Yeah, that's the reason i don't want to empty the cache. It's hard
for people to just undo what the macro does when it performs many
actions, cause then the user sits there hitting the undo button.
What you do is this:
At the beginning of the macro, insert a bookmark
Do your macro
Delete the bookmark
Highjack the Undo function:
Undo
See if the bookmark exists;
If it does, undo again;
Repeat until the bookmark does not exist anymore.
Only one catch: you cannot highjack the undo dropdown list, but you can
highjack the Undo from the Edit menu (or CTRL-Z).
What I do in those case is tell my users to use my Undo button or CTRL-Z,
not the dropdown list on the toolbar.
Sample code:
(The following solution was developped by Roemer Lievaart
<Quote>
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". "
<Unquote>
'_______________________________________
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
'_______________________________________
'_______________________________________
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
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org