Microsoft KB Archive/212698

From BetaArchive Wiki

Article ID: 212698

Article Last Modified on 6/17/2005



APPLIES TO

  • Microsoft Word 2000 Standard Edition



This article was previously published under Q212698


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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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 how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

212536 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:

226118 OFF2000: Programming Resources for Visual Basic for Applications



Additional query words: vb vba vbe

Keywords: kbprb kbdtacode kbpending KB212698