WD: Value Out of Range Error Using WordBasic BookmarkName$

ID: Q173811

The information in this article applies to:

SYMPTOMS

When you use the WordBasic function BookmarkName$(), you may receive the following error message:

   Value out of range

CAUSE

This error message occurs when you attempt to use the BookmarkName$() function with the number of a hidden bookmark.

WordBasic (Word 6.x and 7.x):

For example, the following macro returns a "Value out of range" error message when the active document contains a Table of Contents:

   Sub MAIN
      x = SelInfo(29)
      BK$ = BookmarkName$(x)' <-- The error occurs at this line.
      MsgBox BK$
   End Sub

NOTE: You will receive the same error in Word 97 if you convert this macro to Visual Basic for Applications, as follows:

   Sub MAIN()
      x = WordBasic.SelInfo(29)
      BK$ = WordBasic.BookmarkName$(x)' <-- The error occurs at this line.
      MsgBox BK$
   End Sub

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/ 

To work around this problem, you can trap the error as shown in the following macro example.

WordBasic (Word 6.x and 7.x):

   Sub MAIN
         ' Set an Error trap for the hidden bookmark.
         On Error Goto hiddenbmkerror

         ' Display the name of the bookmark for the selected text.
         MsgBox BookmarkName$(SelInfo(29))

      hiddenbmkerror: ' This line must be left-aligned!
         ' If "Value Out of Range" error...
         If err = 512 Then
            ' then display a message indicating the cause of the error.
            MsgBox "Cannot return hidden Bookmark Names."
         Else
            ' If the error number is not 512, display the error.
            Error Err
         End If
   End Sub

Visual Basic for Applications (Word 97):

The equivalent Visual Basic for Applications command to display the name of a hidden bookmark works as expected in Word 97, so that error trapping is not needed. This command is as follows:

   MsgBox ActiveDocument.Bookmarks(Selection.BookmarkID).Name

MORE INFORMATION

BookmarkName$(Count)

The BookmarkName$() function in WordBasic returns the name of the bookmark specified by Count.

   Argument    Explanation
   ---------------------------------------------------------------------

   Count       The number of the bookmark, from 1 to the total number of
               bookmarks defined for the active document. You can obtain
               the total using CountBookmarks(). The order of the bookmarks
               in the document determines the order of bookmark names.
               You must specify Count; otherwise, the function
               returns an error. For example, a$ = BookmarkName$()
               generates an error.

SelInfo(Type)

The SelInfo() function in WordBasic returns one of 37 types of information about the selection, as specified by Type.

SelInfo(29) returns the number of the bookmark enclosing the start of the current selection. It returns the value 0 (zero) if the selection is not part of a bookmark, or if the selection is not valid. The number returned corresponds to the position of the bookmark in the document, for example, 1 is the first bookmark, 2 is the second, and so on.

REFERENCES

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

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: 8.0 8.00 vba
Keywords          : kberrmsg kbmacro kbprg kbdtacode kbmacroexample word8 winword word6 word7 
Version           : WINDOWS:6.0,6.0a,6.0c,7.0,7.0a
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: April 9, 1999