MS Word

Return to the Last Editing Position When Opening a Document

The Problem:

I began word processing using a Mac application called WriteNow. When you saved a document in which you were working, WriteNow also saved where you were (that is, where your cursor was positioned in the document). When you opened the document again to continue, the cursor started off where you had left it at the end of the previous session. It frustrates me with Word, especially with a loooong document, to always find myself back at the first character of the file when I reopen it.

The Solution:

A macro can make Word return to the last editing position. If you assign the macro the predefined name AutoOpen, it will run automatically when you open a document.

Predefined macro name

Word recognizes five predefined names for macros that run automatically:

  • AutoExec (when Word starts)

  • AutoExit (when Word quits)

  • AutoNew (when you create a new document based on a template containing an AutoNew macro)

  • AutoOpen (when you open a document that contains an AutoOpen macro, or a document based on a template that contains one)

  • AutoClose (when you close such a document)

By assigning these names, you can make macros run automatically. You can also make an automatic macro run another macro if needed. To do so, include the other macro's name on its own line. For example:

    Sub AutoOpen()
    End Sub

How to create a macro:

  1. Open the Visual Basic Editor and create a new module for storing your macros. (Word puts recorded macros in the NewMacros module by default, but you'll do better to keep your macros separate.) Right-click the Normal item in the Project Explorer and choose Insert » Module. The Visual Basic Editor creates a new module called Module1. Press F4 to move the focus to the Properties window, type a distinctive name for the module (again, no spaces, but underscores are okay), and press Enter to apply it.

  2. With your new module still selected in the Project Explorer, click in the Code window. Create the stub of a macro by typing the Sub keyword and the AutoOpen name:

        Sub AutoOpen
  3. Press Enter at the end of the line. The Visual Basic Editor automatically adds the parentheses after the macro name, a blank line, and the End Sub line:

        Sub AutoOpen()
        End Sub
  4. Type the statement Application.GoBack between the Sub line and the End Sub line.

It's as easy as that. If you don't always want to return to the last editing position when you open a document, add a message box to let you choose whether to do so, as shown in Example 8-1.

To prevent a line of code from growing too long, you can break it by typing a space and an underscore at the appropriate point. The break must be between VBA terms rather than inside them, and it cannot be within a string.

AutoOpen macro to return to the location of the last edit after soliciting confirmation

Example 8-1
    Sub AutoOpen()
      If MsgBox(Prompt:="Return to the last edit?", Buttons:=vbYesNo + vbQuestion, _
           Title:="Return to Last Edit") Then
      End If
    End Sub