Undo VBA

D

Derek

I have several VBA routines that execute a number of
functions. If a user decides to undo the action, they
must undo all of the internal VBA functions. Is there a
way to group these and include them in a single undo
action?
 
J

Jonathan West

Derek said:
I have several VBA routines that execute a number of
functions. If a user decides to undo the action, they
must undo all of the internal VBA functions. Is there a
way to group these and include them in a single undo
action?

Unfortunately not.
 
L

Larry

Hi Derek,

There is a way to do this. I don't guarantee that these are perfect
since I've messed around with them a bit. Also, I started to have a
problem whereby when I used Ctrl+Z it would occasionally undo many steps
at once. It's possible that instead of creating macros named EditUndo
and EditRedo, thus changing the built-in commands, it might be better to
give these macros different names from the built-in commands so that
they just have the specialized function of undoing/redoing macros, while
leaving the regular EditUndo/editRedo commands in their default state.

In fact, that's what I've just done myself, and what I would recommend.
Change the macro EditUndo to UndoMacro and change the macro EditRedo to
RedoMacro, and then assign keystrokes to each one, say, Ctrl+Shift+Z to
UndoMacro and Ctrl+Shift+Y to RedoMacro. Then you're not changing the
functionality of the regular Undo/Redo commands and you won't have any
problem with them. Here goes:


1. Put this at the top of your module

Option Explicit

2. Then install these macros.

' NOTE: These macros were provided by Roemer Lievaart
' at (e-mail address removed). It enables Ctrl+Z to undo an
' entire macro in one step. It was on a web page.

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. Loops EditUndo until previous macro is undone.

If ActiveDocument.Undo = False Then Exit Sub

Do While ActiveDocument.Bookmarks.Exists("_InMacro_")
If ActiveDocument.Undo = False Then Exit Sub
Loop

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

3. Then, in any macro that you want to be able to undo in one step, you
do this:

Application.Run "StartUndoSaver"

' your code

Application.Run "EndUndoSaver"
 
L

Larry

A further advantage of changing the macro from EditUndo to UndoMacro (or
whatever name you prefer) is that if you do want to undo a macro step by
step instead of all at once, you still have the option of doing that,
using the regular Undo command.

Larry
 
D

Derek

Thanks for the help. I will give this a try.
-----Original Message-----

Hi Derek,

There is a way to do this. I don't guarantee that these are perfect
since I've messed around with them a bit. Also, I started to have a
problem whereby when I used Ctrl+Z it would occasionally undo many steps
at once. It's possible that instead of creating macros named EditUndo
and EditRedo, thus changing the built-in commands, it might be better to
give these macros different names from the built-in commands so that
they just have the specialized function of undoing/redoing macros, while
leaving the regular EditUndo/editRedo commands in their default state.

In fact, that's what I've just done myself, and what I would recommend.
Change the macro EditUndo to UndoMacro and change the macro EditRedo to
RedoMacro, and then assign keystrokes to each one, say, Ctrl+Shift+Z to
UndoMacro and Ctrl+Shift+Y to RedoMacro. Then you're not changing the
functionality of the regular Undo/Redo commands and you won't have any
problem with them. Here goes:


1. Put this at the top of your module

Option Explicit

2. Then install these macros.

' NOTE: These macros were provided by Roemer Lievaart
' at (e-mail address removed). It enables Ctrl+Z to undo an
' entire macro in one step. It was on a web page.

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. Loops EditUndo until previous macro is undone.

If ActiveDocument.Undo = False Then Exit Sub

Do While ActiveDocument.Bookmarks.Exists("_InMacro_")
If ActiveDocument.Undo = False Then Exit Sub
Loop

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

3. Then, in any macro that you want to be able to undo in one step, you
do this:

Application.Run "StartUndoSaver"

' your code

Application.Run "EndUndoSaver"








.
 
L

Larry

In my own tests, it now works perfectly. The main thing, once again, is
to give the two macros their own names so that they don't affect the
built-in commands.

Larry
 
H

Howard Kaikow

In general, it's not possible because not all Word VBA commands are
UNDOable.
There used to be a list of some of those commands in a MSFT KB article.
 

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