Hi Martin,
Try the solution below: (posted in microsoft.public.word.word97vba on 15th
Jan)
--
Regards
Mike Petrie
WordStar Add-In for Word Home Page
http://www.petrie.u-net.com/
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
lack of time! Thanks!
Regards,
Rmr.
--
----------------------------------------------------------------------------
---
Roemer Lievaart |
don't give in to spam | Recycle je geluk.
----------------------------------------------------------------------------
---
http://listen.to/roemer http://listen.to/vuorkest
----------------------------------------------------------------------------
-----------------
Quote:
> Is it possible to setup an "Undo Range" in a macro... so that a given
> sequence of steps are undone if the user sends the undo command?
> I.e.
> <<begin undo range>>
> Dim rng As Range
> bkmrk = "TextHere"
> ActiveDocument.Bookmarks(bkmrk).Select
> Selection.Delete
> ActiveDocument.Bookmarks.Add (bkmrk)
> Selection.TypeText faxToName
> Set rng =
> ActiveDocument.Range(Start:=ActiveDocument.Bookmarks(bkmrk).Start,
> End:=ActiveDocument.Bookmarks("\Sel").Start)
> ActiveDocument.Bookmarks.Add Name:=bkmrk, Range:=rng
> <<end undo range>>
> All the steps in the range should be undone if the user "says" "Undo"
after
> the macro that performs this code.
> --
> Martin Schmid
> IT Applications Developer
> KtaNet - Network Design
> KTA - Mechanical, Electrical, Plumbing, and Telecommunications Engineering