How to get all the bookmark names in a word file in C#

V

vincent

How to get all the bookmark names in a word file in C#
Many thanks for your replying.
 
S

Shauna Kelly

Hi Vincent

In VBA you can use something like this:

Dim oDoc As Word.Document
Dim bm As Word.Bookmark

Set oDoc = Word.ActiveDocument

For Each bm In oDoc.Bookmarks
Debug.Print bm.Name
Next bm

I'll leave you to translate to C#.

You might like to know that Word accumulates hidden bookmarks as it goes
about its work. And a user or developer might have created a hidden
bookmark. You can see them at Insert > Bookmark, by ticking the hidden
bookmarks box.

If you only want to show the unhidden bookmarks, try something like this:

Dim oDoc As Word.Document
Dim bm As Word.Bookmark
Dim bWasHiddenBookmarks as Boolean

Set oDoc = ActiveDocument

bWasHiddenBookmarks = oDoc.Bookmarks.ShowHidden
oDoc.Bookmarks.ShowHidden = False

For Each bm In oDoc.Bookmarks
Debug.Print bm.Name
Next bm

oDoc.Bookmarks.ShowHidden = bWasHiddenBookmarks


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top