Results 1 to 5 of 5
  1. #1
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Bookmark Name rules (Word2000 sr1a)

    I am creating hidden bookmarks in a word document.

    Both the Object Browser and Word Help say that Bookmark.Name must be a string, but do not give any other requirements.

    I have discovered the hard way that the name can't contain a wide range of characters (including space, dot, parentheses). If I specify a string longer than 32 characters then it gets silently truncated, without generating an error.

    Does anyone know the rules?

    Any offers of a short piece of VBA code that will take a string and remove characters that aren't allowed.

    StuartR

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmark Name rules (Word2000 sr1a)

    This is a snippet of code I wrote ages ago for WordBasic in a routine that makes a bookmark out of the current selection.

    At the time there was no Replace Funtion... so NoIllegal was a homebrew version. Convert it to something like:

    x = replace(k$, Illegal$, "_")

    <pre>[...]
    k$ = WordBasic.[Selection$]()
    'Build a string that holds all the characters that are illegal in a bookmark name
    IllegalString$ = Chr(9) + Chr(10) + Chr(13)
    For x = 32 To 47
    IllegalString$ = IllegalString$ + Chr(x)
    Next x
    For x = 58 To 64
    IllegalString$ = IllegalString$ + Chr(x)
    Next x
    Y = Len(IllegalString$)
    'Step through the illegal string one character at a time
    'and make sure it is replaced in our bookmark with the underscore
    For x = 1 To Y
    Illegal$ = Mid(IllegalString$, x, 1)
    If InStr(k$, Illegal$) Then NoIllegal k$, Illegal$
    Next x
    'remove any trailing underscroe
    If Len(k$) > 1 Then k$ = Trim_$(k$, "_")
    [...]
    </pre>


  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Bookmark Name rules (Word2000 sr1a)

    Thanks, that looks more sensible than my current code of...
    <pre> str = replace(str, "(", "_")
    str = replace(str, ")", "_")
    str = replace(str, "!", "_")
    etc...
    </pre>

    StuartR

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Bookmark Name rules (Word2000 sr1a)

    The "documented" limit for a bookmark is 40 characters.

    AFAIK, a bookmark may contain only letters, digits, and underlines; and must start with a letter or an underline.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Bookmark Name rules (Word2000 sr1a)

    Thanks to all for your help. In case anyone else stumbles on this thread and wants some working code, here is the function I ended up with...

    <pre>Private Function StringToBookmarkName(strName As String) As String

    Const strValidChars As String _
    = "0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkl mnopqrstuvwxyz"
    Dim i As Integer
    Dim strTemp As String
    Dim strNextChar As String

    For i = 1 To Len(strName)
    strNextChar = Mid(strName, i, 1)
    If InStr(1, strValidChars, strNextChar, vbTextCompare) Then
    strTemp = strTemp & strNextChar
    Else
    strTemp = strTemp & "_"
    End If
    Next i

    StringToBookmarkName = strTemp

    End Function
    </pre>

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •