GoBack (Shift+F5) doesn't work in some newly-opened documents
Article contributed by Andria Pinson and Dave Rado
Background
One of the most useful Word shortcuts is Shift+F5, which invokes the GoBack command and returns the selection to the previous editing point. It's useful both while editing (for instance, having copied something, to return to where you were typing previously in order to paste); and when you open a document, in order to return to where you edited it last.
When editing, Shift+F5 goes back to up to three editing points, and when you press it a fourth time, it returns to where you started. When you open a document, it only “remembers” the last editing point.
Either way, it works by storing a built-in bookmark called \PrevSel1 in the document, which it can then return to; and in fact, instead of using the Shift+F5 shortcut, you can select Edit + Go To (or press F5), select “Bookmark” in the left pane, type \PrevSel1 on the right, and click the “Go to” button. (Here's a challenge for you – try to find the list of built-in bookmarks in Word's Help! Given up? Fortunately, they are all listed in Microsoft Knowledge Base article Q212555.)
Some people like to take advantage of this by having Word automatically open their most recent file when they open Word, and retuning the selection automatically to the last editing point, so they can carry on typing where they left off. For details of how to do this, see: How to get the most recently used document to be opened automatically when you open Word.
The bug
If using Word 2000 or higher, you will notice that some files you open won't “go back” when you press Shift+F5.
This is because in Word 2000 and above, the \PrevSel1 bookmark isn't set in the last document that's closed when you exit Word, if you select “Yes” to save the changes to the document.
In fact, in this scenario, even if the \PrevSel1 bookmark exists prior to closing the document, it's actually destroyed when the document is saved!
This bug, which doesn't apply in Word 97, is a little
reminiscent of the bug described in the first paragraph of the article:
A Pseudo DocumentBeforeClose Event).
The fix
Put the following code into Normal.dot:
Option Explicit
Dim
DocWasClosed As Boolean
Sub
DocClose()
'Set a variable so that the AutoClose
macro knows not to quit Word
On Error Resume Next
DocWasClosed = True
WordBasic.DocClose
If Err
Then
DocWasClosed = False
End Sub
Sub FileClose()
'Set a variable so that the AutoClose
macro knows not to quit Word
On Error Resume Next
DocWasClosed = True
WordBasic.DocClose
If Err
Then
DocWasClosed = False
End Sub
Public Sub AutoClose()
On Error Resume Next
If Documents.Count = 1
Then
'make sure the
doc is not in another application such as IE, Outlook, etc
If Not
ActiveWindow.Caption = "" Then
'make sure it's "dirty"
If Not ActiveDocument.Saved Then
'Make sure it was not closed by invoking DocClose or FileClose commands
If Not DocWasClosed Then
'Create an extra "dummy" document, _
to prevent the "last doc" bug from manifesting
Documents.Add Visible:=False
'Can't quit Word yet or you get an error, so use OnTime
Application.OnTime When:=Now, Name:="FinishOffAutoClose"
End If
End If
End If
End If
'Reset the variable
DocWasClosed = False
End Sub
Sub FinishOffAutoClose()
Application.Quit
End Sub
Sub FileExit()
Application.Quit
End Sub
Notes: You have to use AutoClose rather than AutoExit or DocumentBeforeClose, because the latter both fire after the “Do you want to save changes” messages. Unfortunately, this means the code must be stored in Normal.dot (or in all your templates, but it can't be stored in an Add-in).
The FileExit macro is required because without it, invoking File + Exit won't allow you to cancel out of the "do you want to save changes" dialog, for some weird reason!