Returning to the last editing spot in Word 2007

Many Word 2007 users miss the ability to press Shift+F5 to return to the last place editing occurred in a document they open. Shift+F5 executes Word’s built-in GoBack command. It continues to work just fine in a document you’re editing, cycling among the current and last three places editing occurred. However, it does not work for a document you’ve just opened in Word 2007 (this appears to be fixed in Word 2010, but some add-ins can interfere with Word’s “remembering”).

If you’re up for a little bit of very simple VBA programming, you can create the ability to automatically return to the spot where the cursor was the last time the document was saved. It works like this. When you close a document based on Normal.dotm, if you have a macro named AutoClose, that macro gets executed each time you close a document. When you open a document, if you have a macro named AutoOpen, that macro gets executed.

Note that you can set this up in other templates as well, but putting the system into the default global template—Normal.dotm—will handle most of the documents most people edit.

So, the first step is to create AutoClose (if you don’t already have an AutoClose macro), and include in it the instruction to insert a bookmark. I named this bookmark LastEdited. You could call it whatever you want. You could call it UncleFreddy or AuntPetunia. It doesn’t matter, as long as the act of setting it is contained in an AutoClose macro. The macro looks like this, at minimum:

Sub AutoClose()

    On Error Resume Next

    ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=”LastEdited”

End Sub

So, each time you close a document based on the template that contains the AutoClose macro, a bookmark named LastEdited is created. I’ll bet you can see where this is going.

Each time you open a document, if the underlying template contains a macro named AutoOpen, it gets executed. So, guess what we’re going to have that AutoOpen macro do! Right! We’re going to have it take us to the LastEdited location. At a minimum, the AutoOpen macro will look like this:

Sub AutoOpen()

    On Error Resume Next

    Selection.GoTo What:=wdGoToBookmark, Name:=”LastEdited”

End Sub

That’s it! Creating the macros and setting the security is up to you. I said “at minimum,” because you might want your AutoOpen macro to do other things, like set the zoom at 140%, put the file location into the title bar, or fix you a cup of tea. These frills are entirely up to you.

You might be wondering what the On Error Resume Next is for. That’s there in case the macro encounters some kind of problem. The first time you open a document, for example, there will be no LastEdited bookmark. Without the error handling statement, you’d get a nastly little dialog box. Or, what happens if you’re editing a protected document and can’t create a bookmark. Same deal. The error handling statement keeps you from getting an error message.

One side effect of this system is that you will always be asked whether to save the file. That’s because inserting the bookmark is an edit. If you say No to saving changes, then the bookmark will not be saved.

So, what happens if there’s already a bookmark named LastEdited? Simple—it gets overwritten by the new one. If you think there’s some chance that there’s an unrelated bookmark named LastEdited in any of your documents, then give this one a different name… like TheInsertionPointLocationTheLastTimeThisDocumentWasSaved.

This entry was posted in Uncategorized. Bookmark the permalink.

11 Responses to Returning to the last editing spot in Word 2007

  1. EdgarPS says:

    I’m not sure what I am doing wrong but I thought I followed the instructions to the letter but ended up with a macro that wouldn’t compile.

  2. herbt3 says:

    What do yo mean by compile with respect to a VBA macro. While there might be some kind of internal compilation that goes on, there is no compile step (at least not in the traditional sense). Are you saying that you get an error when you try to run the macro? If so, what’s the error message?

  3. EdgarPS says:

    Everything seems to go according to plan until I type in the Range:=Selection.Range, part. Then the line turns red and an error box pops up saying

    Compile error
    Expected: named Parameter

    Does this make any sense to you?

  4. herbt3 says:

    I think I see the problem. The following should all be one line:

    ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=”LastEdited”

    Lines sometimes break in unfortunate/confusing ways in blogs.

  5. EdgarPS says:

    It works like a charm – thanks very much!

  6. herbt3 says:

    Glad it worked out!

  7. Cathy says:

    Great help. Thanks for the resource.

  8. Mike Fox says:

    Dear Herb Tyson,

    I have followed your instructions and as far as I can discern (not being computer ‘trained’ rather than learning empirically). It seems the AutoClose macro (saved in normal.dotm) that I have ‘created’ is simply not creating the necessary LastEdited bookmark which AutoOpen must find to place the cursor there. I would really appreciate some further advice (hopefully written for a half-educated user). I very much admire the help you already offer and am grateful for it.

    Yours faithfully,

    Mike Fox
    London, Eng.

  9. herbt3 says:

    The only think that occurs to me is that the following needs to all be on one line in the macro:

    ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:=”LastEdited”

    WordPress sometimes breaks lines in awkward ways.

  10. Abhi says:

    Thanks, I tried it and it worked. I wish it was a bit ‘watered down’ to be intelligible for non-specialists. Maybe a couple of screenshots would help.

  11. herbt3 says:

    Thanks for the suggestion. Sometime in the near future, I’ll put together an article about how to create a macro, set up a signing certificate, and set macro security, and then link it from any macro articles I write. As you might imagine, however, to really cover all the steps, that kind of article can get very involved. Graham Mayor has an article here about installing macros:

Leave a Reply