Kevin
Kevin

Reputation: 205

VB.NET Microsoft Office Interop Bookmarks

I am working on a project which will include automatically filling out some templates. I plan to use word bookmarks to create the templates and then fill them in via VB. This would be no problem, but the problem is that I would like to allow other templates to be used. Is there a way I can open a word document and get all the bookmarks out of it? I need a list, so I can determine which ones I am able to fill out, then send the correct values. Here is the code I am working with if you need a refresher.

    Imports Microsoft.Office.Interop


    Dim oWord As Word.Application
    Dim oDoc As Word.Document

    oWord = CreateObject("Word.Application")
    oWord.Visible = True
    oDoc = oWord.Documents.Add("Z:\DJ\Documents\Reminder_Letter.doc")
    oDoc.Bookmarks("full_name").Range.Text = "John Smith"

Basically, I just want to make sure that "full_name" exists in a document before I try to add a value to it, so I need a list of bookmarks in the document.

Thanks, Kevin

Upvotes: 2

Views: 5612

Answers (2)

Nicolas
Nicolas

Reputation: 46

It will be faster to use the Exists function of the bookmark's collection :

 oDoc.Bookmarks.Exists("bookmark name")

Upvotes: 3

DarinH
DarinH

Reputation: 4889

Just iterate through the bookmarks collection, checking the name.

Something like

   For each bm in oDoc.bookmarks
       if bm.Name = "blah" then
           'this is my bookmark
       end if
   next

Upvotes: 3

Related Questions