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:
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
wrdBookmark.Range.Text = Date
wrdBookmark.Range.Text = frmSearch.flxList.TextMatrix(n, 3)
wrdBookmark.Range.Text = "The meeting date here"
wrdBookmark.Range.Text = "The meeting time here"
wrdBookmark.Range.Text = "The address here"
wrdBookmark.Range.Text = "The city here"
wrdBookmark.Range.Text = "The country date here"
wrdDoc.SaveAs (App.Path & "\Merge Letters\" & strPath & "\" & frmSearch.flxList.TextMatrix(n, 0))
wrdApp.DisplayAlerts = wdAlertsNone
msg = MsgBox("Mail merge completed successfully.", vbInformation, "Mail Merge")