Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion GOOD CODE: How to save your users from a terrible "undo-mess"
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Roemer Lievaart  
View profile  
 More options Jan 14 2000, 3:00 am
Newsgroups: microsoft.public.word.word97vba
From: lieva...@dds.nl (Roemer Lievaart)
Date: 2000/01/14
Subject: GOOD CODE: How to save your users from a terrible "undo-mess"
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
(lieva...@dds.nl) too! I do not read this newsgroup anymore due to
lack of time! Thanks!

Regards,

Rmr.

--
--------------------------------------------------------------------------- ----
Roemer Lievaart         |
lieva...@dds.nl         |                    Leef bewust:
don't give in to spam   |                 Recycle je geluk.
--------------------------------------------------------------------------- ----
        http://listen.to/roemer            http://listen.to/vuorkest


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google