Re: grouping actions for undo

From: Jean-Guy Marcil (no-spam_at_leaveme.alone)
Date: 03/12/04


Date: Fri, 12 Mar 2004 10:35:12 -0500

Hi Roel,

You can do it by using the following code.
The principle is that just before you start your procedure you create a
unique bookmark, then do the procedure, then delete the bookmark.
After that, you have to catch the CTRL-Z or CTRL-Y events. When you catch
them, you check for the existence of the said bookmark. if it is found, it
undoes (or redoes) until it is gone, effectively undoing (redoing) many
actions in one go.

I wish I could take credit for this brilliant idea...but I can't.
Unfortunately, I do not remember where I got this from... Apologies to
original creator.

'_______________________________________
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
"Roel Vanhout" <roel@riks.nl> a écrit dans le message de news:
ufRA3UCCEHA.3308@TK2MSFTNGP10.phx.gbl...
> Hello all,
>
> This is my first attempt at writing vba things, so please excuse me if
> I'm asking obvious things here.
> I'm trying to do the following: when a button in the toolbar is pressed,
> a form should show up, with some field that can be filled in. When 'ok'
> on that form is clicked, the fields should be entered into the word
> document with some formatting applied. I have the form running and I can
> insert text. Because I need to do formatting I'm inserting every part
> with Selection.InsertAfter and then I apply the formatting to that part.
> So far so good, but I'd like to make it so that when a user does this
> and presses 'undo' that the whole thing I just inserted is removed
> again. Because of the multiple edit actions that I do, the user has to
> undo every action that I did programmatically, which is around 20 or so
> (for now). How can I 'group' editing actions so that they all get undone
> simultaniously? Thanks.
>
> cheers,
>
> roel