Undo Macro Actions

R

rhamre

I was wondering if there was a way, or any better logic, in a macro that will
undo what the previous macro did.

So far, this is what i thought of.

At the beginning of every macro, i clear the undo's cache.

Then, if a person want's to undo what the macro did, they'll have a button
that goes to this macro-

Sub UndoMacro()

Do
ActiveDocument.Undo (1)

Loop While ActiveDocument.Undo = True

End Sub

Now, is there an easier way than this?

I would also like it if it didn't clear the cache, so the cache was still
there and if the person wanted, they could undo what the macro did, and if
needed, undo whatever else they did before that.

Thanks.
 
C

Charles Kenyon

I would be very careful of emptying the undo cache. The user can currently
undo the macro just by using undo.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
R

rhamre

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.
 
J

Jean-Guy Marcil

(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
 
J

Jean-Guy Marcil

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :
(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :


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

Sorry, if you see underlined text in the macro code, replace all the
underlined "InMacro" with this:

(Underscore Character)InMacro(Underscore Character)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

rhamre

(Underscore Character)InMacro(Underscore Character)

The above code totally didn't work.

I replaced "_InMacro_" with "(Underscore Character)InMacro(Underscore
Character)"

And that code didn't work. But i left the code with the underscores and that
did work.

Now, i have a macro where i do cutting and stuff, and i have this macro, and
i can see the bookmarks in my undo dropdown list, but it doesnt undo back to
the bookmark, it goes back to where i cut the document.

I'm going to do some tweaking, but thanks for all the help guys, it got me
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
(Underscore Character)InMacro(Underscore Character)

The above code totally didn't work.

lol
I meant for you to replace (Underscore Character) by _
I just mentioned it because some news browsers replace a word sandwiched by
two underscore characters by an underlined word.
I replaced "_InMacro_" with "(Underscore Character)InMacro(Underscore
Character)"

And that code didn't work. But i left the code with the underscores
and that did work.

Now, i have a macro where i do cutting and stuff, and i have this
macro, and i can see the bookmarks in my undo dropdown list, but it
doesnt undo back to the bookmark, it goes back to where i cut the
document.

It should work. Use the example I provided and run it as is to see how it
works and to familiarize yourself with the logic.
Also, if you cut the place where you inserted the bookmark the macro will
stop running because the bookmark does not exist as soon as you cut the
range of text where it was created.

I have used this many times without fail.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

rhamre

Ahhh gotcha, i was really confused cause in all the browsers i used (opera,
firefox, IE) all i saw were the actual underscores, so i thought you meant
change the underscores :)

As for the "cutting" i do cut the document, so that's what was happening.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top