Word VBA: How to undo all actions of a macro with one 'Undo'?

B

bourgui

Hi all,

I have written macros that, among other things, set some bookmarks
(working
in pairs) through a document that I use later with my tool.

The problem I have is that the actions of my macros are undone one by
one,
So a user can launch the macro, then type some text or whatever else,
then
decide to 'Undo' what they have just done. If they click one (or more)
'Undo'
too many, they can for example delete one of the bookmarks from a
pair.
Deleting a bookmark does not have a high-visual impact, so it usually
goes
unnoticed until they call a macro which is supposed to use it and
fails.

I give the example of a bookmark but it can be many other things, such
as
hidden text insertion, font change...

Is there any way to group the actions of a macro (and all the
functions it
may have called) so that they are all considered one action when it
comes to
'Undo' and 'Redo'?

Powerpoint (and Excel?) actually works the way I would like it to do
in Word.

Thanks!
 
B

bourgui

        Hi all,

I have written macros that, among other things, set some bookmarks
(working
in pairs) through a document that I use later with my tool.

The problem I have is that the actions of my macros are undone one by
one,
So a user can launch the macro, then type some text or whatever else,
then
decide to 'Undo' what they have just done. If they click one (or more)
'Undo'
too many, they can for example delete one of the bookmarks from a
pair.
Deleting a bookmark does not have a high-visual impact, so it usually
goes
unnoticed until they call a macro which is supposed to use it and
fails.

I give the example of a bookmark but it can be many other things, such
as
hidden text insertion, font change...

Is there any way to group the actions of a macro (and all the
functions it
may have called) so that they are all considered one action when it
comes to
'Undo' and 'Redo'?

Powerpoint (and Excel?) actually works the way I would like it to do
in Word.

Thanks!

I forgot to mention that this is for Office XP/2003 and 2007.

Thanks!
 
J

Jay Freedman

That's a nice solution, but it's for Project 2007 only. The original question
was about Word, which doesn't have any of the Application methods required for
that solution.

Note that the question was answered on 8 September 2008 when it was posted:
http://groups.google.com/group/micr...2f55f6d231dd?hl=en&lnk=st&q=#1e542f55f6d231dd

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
Z

zskillz

Hi Jay,
I'd very much like to see that solution, but I'm getting a "403 Error -
.... but your query looks similar to automated requests from a computer virus
or spyware application. To protect our users, we can't process your request
right now. " from google.

can you link to that thread in another fashion?
Thanks
z
 
J

Jay Freedman

Hi Jay,
I'd very much like to see that solution, but I'm getting a "403 Error -
... but your query looks similar to automated requests from a computer virus
or spyware application. To protect our users, we can't process your request
right now. " from google.

can you link to that thread in another fashion?
Thanks
z

I don't have any trouble getting to the thread that I linked to, or to the
thread linked there, so it might have been a temporary glitch at Google. But
since the original post was on 14 Jan 2000, and might someday "fall off" the
Google Groups archive, I'll repost it here. I hope the original poster, Roemer
Lievaart, doesn't mind. :)

~~~~~~

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
([email protected]) too! I do not read this newsgroup anymore due to
lack of time! Thanks!


Regards,


Rmr.


--
---------------------------------------------------------------------------­----
Roemer Lievaart |
(e-mail address removed) | Leef bewust:
don't give in to spam | Recycle je geluk.
---------------------------------------------------------------------------­----
 

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