WD2000: GoBack Method Switches to Another Document

ID: Q212698


The information in this article applies to:


SYMPTOMS

When you use the GoBack method in a Microsoft Visual Basic for Applications macro, or when you press SHIFT+F5, Word may switch to another open document.


CAUSE

This behavior occurs because the Visual Basic for Applications GoBack method and the SHIFT+F5 keystroke operate at the Word session level rather than at the Word document level. The Visual Basic for Applications GoBack ethod switches among the last three locations in the currently open documents in which text or formatting has changed.

If only one document is open, the GoBack method switches among the last three editing positions in the current document.

When you run a macro that uses the GoBack method and you have more than one document open in Word, the insertion point may go to an unexpected location. For example, if you use the GoBack method in an AutoOpen macro, the insertion point may go to another document instead of going to the last editing position within the document you are opening.


WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
To work around this problem and return to the previous editing location within a document, use one of the following methods.

Method 1: Set a permanent bookmark.

When you run the macro, the insertion point goes to this bookmark location.

The following macro creates a bookmark called "mark" at the insertion point.

NOTE: If the document that contains the bookmark is not open, you receive an error message.

Sub SetBookMark()
   On Error Resume Next
   Selection.Bookmarks.Add Name:="mark"
   If Err >0 Then MsgBox Err.Description
End Sub 
The following macro returns the insertion point to the bookmark you set.

Sub GoToMark()
   On Error Resume Next
   Selection.GoTo What:=wdGoToBookmark, Name:="mark"
   If Err > 0 Then MsgBox Err.Description
End Sub 
For more information about adding bookmarks, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Add Bookmark in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Method 2: Set a named range.

The named range exists only while the macro is running. You can return to the named location from within any document while the document that contains the named range is open.

NOTE: If the document that contains the named range is not open, you receive an error message.

The following macro defines a range called "MyRange" at the insertion point:

Sub SetRangeMark()
   On Error Resume Next
   Set MyRange = Selection.Range
   If Err > 0 Then MsgBox Err.Description
End Sub 
The following macro returns the insertion point to the range set in the previous example:

Sub GoToRangeMark()
   On Error Resume Next
   MyRange.Select
   If Err > 0 Then MsgBox Err.Description
End Sub 
For more information about the Range method, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Range method in the Office Assistant or the Answer Wizard, and then click Search to view the topic.


MORE INFORMATION

The following is a sample AutoOpen macro that uses the GoBack method:


Sub AutoOpen()
   Application.GoBack
End Sub 
For more information about using the sample code in this article, please see the following article in the Microsoft Knowledge Base:
Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles


REFERENCES

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

Q226118 OFF2000: Programming Resources for Visual Basic for Applications

Additional query words: vb vba vbe


Keywords          : kbdta kbdtacode wd2000 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: July 27, 1999