Search code examples
vb.netvisual-studioms-wordoffice-interopbookmarks

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


Solution

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

     oDoc.Bookmarks.Exists("bookmark name")