return document to the way it was at previous save

L

Larry

Is there a way to make a document in Word 97 go in ONE STEP back to the
way it was at the previous save?

I have a roundabout way of doing this: close without saving, then
re-open the document. But I'd like to do it instantly, with a single
command. I also don't want to use the Undo box, figuring out how many
actions to undo.

Thanks,
Larry
 
L

Larry

It occurred to me, here's one way it might be possible to do this, if
there is a way to count the number of actions that are in the Undo list.

If that can be done, then you could have a macro that saves the document
and counts the number of actions in the Undo list, lets say there are 15
actions in the list.

Then, after making various changes in the document, you run a second
macro that counts the current number of actions in the Undo list, lets
say it's now 20, compares that number to the previous number and finds
the difference, which is 5, and undoes five actions.

Possible? :)

Larry
 
J

Jezebel

I think you'll find that closing and re-opening the document is simpler and
more reliable. For one thing, how are you planning to count the number of
actions in the Undo list? It's not available as a property.

The best method I've found for working with the Undo list is to add a
bookmark that points to either the beginning or end of the document (even if
the document is empty, these are different). At the start of the routine --
in your case, when the document is saved -- you toggle the bookmark: if it
points to the start, set it to point to the end, or vice versa. To undo back
to the last save, undo the actions one at a time until the bookmark changes.
Has the advantage that it works in reverse for redo also.

Maybe simpler in your case is just to clear the Undo list when you save.
Then to revert to saved version just undo until the list is empty.
 
L

Larry

Both ideas are good but your second idea is simpler. But is there a way
programmatically to Undo the entire Undo list without either undoing
them one at a time or manually clicking on the Undo list?
 
J

Jonathan West

Larry said:
Both ideas are good but your second idea is simpler. But is there a way
programmatically to Undo the entire Undo list without either undoing
them one at a time or manually clicking on the Undo list?

To clear the undo stack, use this

ActiveDocument.UndoClear

To undo everything on the stack, use this

ActiveDocument.Undo 100

This will undo everything that can be undone. The undo stack has a maximum
of 100 items in it, so if you have gone further than that, you can't get
further without closing the document unsaved and then reopening.
 
L

Larry

However, I realize I don't need a command that undoes the entire list.
I can just set a high number, say

ActiveDocument.Undo(20)

and even if there are only five actions to be undone, the above line
will work to undo the entire list and will not trigger an error.

So the two macros together are

Sub UndoListClearDocumentSave()

If ActiveDocument.Path <> "" Then
ActiveDocument.UndoClear
ActiveDocument.Save
Else
MsgBox "This macro can only be run on a saved document."
End If

End Sub

Sub UndoToLastSave()

ActiveDocument.Undo (20)

End Sub


Thanks much. This will come in handy.

Larry
 
B

Beth Melton

Why not just use the "Always create Backup copy" option under
Tools/Options/Save?

Then if you need to go back just open the backup copy which will be
one version prior to the last save.

--
Please post all follow-up questions to the newsgroup. Requests for
assistance by email can not be acknowledged.

~~~~~~~~~~~~~~~
Beth Melton
Microsoft Office MVP

Word FAQ: http://mvps.org/word
TechTrax eZine: http://mousetrax.com/techtrax/
MVP FAQ site: http://mvps.org/
 
S

Suzanne S. Barnhill

But that might Undo actions that were made before you last saved.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

Larry

That's not a problem because I've already run ActiveDocument.UndoClear,
so I only undo the changes that were made after running the first macro.
 
L

Larry

Yes, that's true, but I just wanted to create a simple method that would
work within the active document.
 
L

Larry

I realized this didn't have to be limited to saved documents. Also, I
thought it would be better to have a message box to have more control
over whether the macros are run, since you wouldn't want to make changes
like this by accident. So I modified the macros as follows:

Sub UndoListClear()
' Clears the Undo list in preparation for making further changes that
can
' all be undone with a single command, UndoRecentChanges.

System.Cursor = wdCursorIBeam
If MsgBox("Clear Undo list.", vbOKCancel, "Undo List Clear") = vbOK Then
If ActiveDocument.Path <> "" Then ActiveDocument.Save
ActiveDocument.UndoClear
End If

End Sub

Sub UndoRecentChanges()

' Undoes all recent changes, and so returns document to condition it was
in after
' macro, UndoListClear, was run.
' The maximum this can be set for is 100. The Undo stack can hold 100
actions. But
' there would never be that many changes with what I'm doing here.

If MsgBox("Undo last 20 changes.", vbOKCancel, "Undo Recent Changes") =
vbOK Then
ActiveDocument.Undo (20)
End If

End Sub
 
K

Klaus Linke

Jonathan West said:
[...] To undo everything on the stack, use this

ActiveDocument.Undo 100

This will undo everything that can be undone.
The undo stack has a maximum of 100 items in it
[...]

I don't think the Undo stack has any limit (except hard disk space).
This should undo everything:

While ActiveDocument.Undo : Wend

(but careful: It undoes every change since you opened the document,
not only every change since you saved the last time)

Regards,
Klaus
 

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