Get collection of Word bookmarks

cpk68
cpk68 used Ask the Experts™
on
Hello all

I'm in a bit of a bind and hope someone can help. I'm merging a word document from VB using bookmarks in the Word document. The problem is that it can be any one of five different bookmarks, so I need to find out which ones are used in the particular document before I can merge it.

I've tried to find a way around it by, intercepting the inevitable error and moving on to the next bookmark, but that didn't quite work.

Thanks in advance

My code is as follows:


Option Explicit
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdBookmark As Word.Bookmark

Private Sub cmdMerge_Click()

Dim, strPath As String, intBookMark As Integer

' create a new work application
   Set wrdApp = New Word.Application
   
For n = 0 To frmSearch.flxList.Rows - 1
       
Set wrdDoc = wrdApp.Documents.Open(App.Path & "\Merge Letters\TestMerge.doc")
   
For intBookMark = 1 To 5
   On Error GoTo NoMoreBookMarks
   Set wrdBookmark = wrdDoc.Bookmarks(intBookMark)
       
      Select Case wrdBookmark.Name
           
         Case "DATE"
            wrdBookmark.Range.Text = Date
                   
         Case "FOA"
            wrdBookmark.Range.Text = frmSearch.flxList.TextMatrix(n, 3)
               
         Case "MeetingDate"
            wrdBookmark.Range.Text = "The meeting date here"
                   
         Case "MeetingStart"
            wrdBookmark.Range.Text = "The meeting time here"
               
         Case "Address1"
            wrdBookmark.Range.Text = "The address here"
               
         Case "City"
            wrdBookmark.Range.Text = "The city here"
               
         Case "Country"
            wrdBookmark.Range.Text = "The country date here"
       
     End Select

NoMoreBookMarks:

Next
     
   wrdDoc.SaveAs (App.Path & "\Merge Letters\" & strPath & "\" & frmSearch.flxList.TextMatrix(n, 0))
       
    Next
   
    wrdApp.DisplayAlerts = wdAlertsNone
    wrdApp.Quit
   
    msg = MsgBox("Mail merge completed successfully.", vbInformation, "Mail Merge")
   
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Your own solution to the problem is nearly the one you need. You already got a handle on the Word.Bookmark object, you just need to use it different.

Your example but then different:

For each wrdBookmark in wrdDoc.Bookmarks
  Select Case wrdBookmark.Name
  Case "DATE"
    wrdBookmark.Range.Text = Date
  Case "FOA"
    wrdBookmark.Range.Text = frmSearch.flxList.TextMatrix(n, 3)
  Case "MeetingDate"
    wrdBookmark.Range.Text = "The meeting date here"
  Case "MeetingStart"
    wrdBookmark.Range.Text = "The meeting time here"
  Case "Address1"
    wrdBookmark.Range.Text = "The address here"
  Case "City"
    wrdBookmark.Range.Text = "The city here"
  Case "Country"
    wrdBookmark.Range.Text = "The country date here"
  End Select
Next
Set wrdBookmark = nothing

Using this structure you enumerate all the bookmarks, and only act if the name is equal to the ones you might need to insert text. This is quite safe, since the loop code never does more then there are bookmarks. Using an error handler to catch a non-existing bookmark can yield other problems with code that generates an error outside your loop.

I hope this helps you.

Grtz.©

D.

Author

Commented:
Thanks alot, that works a treat.
You are welcome...

Grtz.©

D.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial