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.

Re: Undo Macro Actions by Charles

Charles
Thu Dec 15 15:12:04 CST 2005

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

See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!
--------- --------- --------- --------- --------- ---------
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.

"rhamre@citation.com" <rhamrecitationcom@discussions.microsoft.com> wrote in
message news:238F0EA4-8129-4D7F-BFC6-134961A1088C@microsoft.com...
>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.



Re: Undo Macro Actions by rhamrecitationcom

rhamrecitationcom
Thu Dec 15 15:22:02 CST 2005

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.

"Charles Kenyon" wrote:

> 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
>
> See also the MVP FAQ: http://word.mvps.org/FAQs/ which is awesome!
> --------- --------- --------- --------- --------- ---------
> 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.
>
> "rhamre@citation.com" <rhamrecitationcom@discussions.microsoft.com> wrote in
> message news:238F0EA4-8129-4D7F-BFC6-134961A1088C@microsoft.com...
> >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.
>
>
>

Re: Undo Macro Actions by Jean-Guy

Jean-Guy
Thu Dec 15 15:50:38 CST 2005

rhamre@citation.com was telling us:
rhamre@citation.com 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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Undo Macro Actions by Jean-Guy

Jean-Guy
Thu Dec 15 15:55:20 CST 2005

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :

> rhamre@citation.com was telling us:
> rhamre@citation.com 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

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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Undo Macro Actions by Jay

Jay
Thu Dec 15 17:05:25 CST 2005

A few years ago a gentleman named Roemer Lievaart posted an ingenious
method of using a bookmark to track how many Undo actions are needed
to remove the effect of a macro. See if his technique helps:

http://groups-beta.google.com/group/microsoft.public.word.word97vba/msg/c5f120a7817f3dbb?hl=en

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Thu, 15 Dec 2005 12:11:02 -0800, "rhamre@citation.com"
<rhamrecitationcom@discussions.microsoft.com> wrote:

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

Re: Undo Macro Actions by rhamrecitationcom

rhamrecitationcom
Tue Dec 20 14:27:02 CST 2005

(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
on the right track.

"Jean-Guy Marcil" wrote:

> Jean-Guy Marcil was telling us:
> Jean-Guy Marcil nous racontait que :
>
> > rhamre@citation.com was telling us:
> > rhamre@citation.com 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
>
> 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
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>
>
>

Re: Undo Macro Actions by Jean-Guy

Jean-Guy
Tue Dec 20 23:45:46 CST 2005

rhamre@citation.com was telling us:
rhamre@citation.com 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
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org



Re: Undo Macro Actions by rhamrecitationcom

rhamrecitationcom
Wed Dec 21 08:52:03 CST 2005

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.

"Jean-Guy Marcil" wrote:

> rhamre@citation.com was telling us:
> rhamre@citation.com 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
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
>
>
>