9

I need to bookmark parts of a document from the name of paragraphs but the name of a paragraph is not always a valid name for a bookmark name. I have not found on Google or MSDN an exhaustive list of limitations for bookmark names.

What special characters are forbidden?

The only thing I found is that the length must not exceed 40 characters.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • This may be of interest to passers by, how to add a ` . ` to a bookmark in MS Word: https://stackoverflow.com/a/51868419/3451115 – SlowLearner Aug 16 '18 at 01:26

1 Answers1

22

If you are familiar with regular expressions, I would say it is

^(?!\d)\w{1,40}$

Where \w refers to the range of Unicode word characters, which also contain the underscore and the digits from 0-9.

In plain English: The bookmark name must...

  • be between 1 and 40 characters long
  • consist of any combination of Unicode letters, digits, underscores
  • not start with a digit
  • not contain any kind of white space or punctuation

As stated in the comments, bookmark names beginning with an underscore are treated as hidden. They will not appear in the regular user interface, but they can be used from VBA code. It it is not possible to create bookmarks that begin with an underscore via the regular user interface, but you can do it through VBA code with Bookmarks.Add().

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 2
    +1, Plus that bookmarks whose name begins with '_' are treated as hidden. – Dirk Vollmar May 12 '09 at 14:11
  • That would lead us to "^(_|\w)[\w\d]{0,39}$" –  May 12 '09 at 14:29
  • @Maxime Vernier: "_" is traditionally part of "\w". – Tomalak May 12 '09 at 14:30
  • @divo: I actually failed to create a bookmark that begins with "_" in Word 2003... – Tomalak May 12 '09 at 14:33
  • @Tomalak: I have checked the meaning of \w on http://en.wikipedia.org/wiki/Regular_expressions and it seems \d is part of \w too. I would say "^[_A-Za-z][A-Za-z0-9]{0,39}$" - starts with an underscore or a letter - then at most 39 letters or digits –  May 12 '09 at 14:43
  • @Tomalak: I cannot create a bookmark that begins with "_" in Word 2000 or 2003. It should be possible with the API though. –  May 12 '09 at 14:49
  • You can have underscores within the name. Rules: 1. starts with an underscore or a letter, 2. followed with at most 39 letters, digits or underscores. ^[_A-Za-z]\w{0,39}$ –  May 12 '09 at 15:06
  • Maxime Vernier: You are right - \d is part of \w. I'll reformulate the Regular expression. :) – Tomalak May 12 '09 at 15:12
  • @Maxime Vernier: I refer to \w instead of "A-Z" because word characters from foreign languages are explicitly allowed. You may create Hebrew bookmarks, or Chinese ones, for example. – Tomalak May 12 '09 at 15:28
  • @Tomalak: I have trouble reading your last rexexp ^(?!\d)\w{1,40}$. For me it means starts with an optional character (any character: .) except \d. Since we want \w minus \d, it catches more things than needed and it can match strings of length 41. How about ^[_A-Za-z]\w{0,39}$ instead? –  May 12 '09 at 16:02
  • The regular expression contains of a so-called "negative zero-width assertion" (?!\d) meaning "a position not followed by a digit", and 1-40 "word characters". This special construct is necessary because \w{1,40} would allow a string that *begins* with a digit. Your regex would force the first character to be in the A-Z range, but in fact many more characters than this are legal. My variant allows them all. – Tomalak May 12 '09 at 21:55
  • I understand the "negative zero-width assertion" now. My regexp is ascii only, yours is unicode thus better. Thanks to both of you for your help. –  May 13 '09 at 08:33
  • As mentioned above bookmarks starting with an underscore are marked as hidden. Make sure to set the ShowHidden boolean to true on the bookmarks collection to be able to enumerate them. – Benjamin Wegman Mar 21 '11 at 11:19