Link to home
Start Free TrialLog in
Avatar of cpk68
cpk68

asked on

Get collection of Word bookmarks

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
ASKER CERTIFIED SOLUTION
Avatar of daffyduck14mil
daffyduck14mil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cpk68
cpk68

ASKER

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

Grtz.©

D.