Thanks alot, that works a treat.
Main Topics
Browse All TopicsHello 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.
For intBookMark = 1 To 5
On Error GoTo NoMoreBookMarks
Set wrdBookmark = wrdDoc.Bookmarks(intBookMa
Select Case wrdBookmark.Name
Case "DATE"
wrdBookmark.Range.Text = Date
Case "FOA"
wrdBookmark.Range.Text = frmSearch.flxList.TextMatr
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.TextMatr
Next
wrdApp.DisplayAlerts = wdAlertsNone
wrdApp.Quit
msg = MsgBox("Mail merge completed successfully.", vbInformation, "Mail Merge")
End Sub
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: daffyduck14milPosted on 2002-12-17 at 08:21:51ID: 7596045
Hi,
ix(n, 3)
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.TextMatr
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.