Merging Data To Word Error

rockman

Senior Member
Local time
Today, 07:47
Joined
May 29, 2002
Messages
190
I'm merging about 10 strings from a form to a number of different Microsoft Word templates. The problem is that a particular template may not need all 10 strings and therefore doesn't have all 10 bookmarks. I get an unexpected error if a particular bookmark doesn't exist.

My code looks like this:
Code:
On Error Resume Next
With WordApp.Selection
.GoTo what:=wdGoToBookMark, Name:="Full Name"
If Err.Number = 0 Then .TypeText txtFullName.Value
.GoTo what:=wdGoToBookMark, Name:="DOB"
If Err.Number = 0 Then .TypeText DOB.Value
.GoTo what:=wdGoToBookMark, Name:="SSN"
If Err.Number = 0 Then .TypeText txtSSN.Value
End With
Everything works fine if all of the bookmarks are in the Word template. However, for example, if a particular template doesn't have a DOB bookmark, an error #5101 "This bookmark does not exist." is generated. (This is to be expected and the code is set-up to handle this). However, when the next bookmark GoTo is called (in this example, .GoTo what:=wdGoToBookMark, Name:="SSN"), the code generates an error #5099 "You can specify only one line, footnote, endnote, comment, or field at a time." All subsequent GoTo's also generate the #5099 error.

Any ideas why I'm generating the #5099 error. As usual the error description isn't helping me very much.

Thanks for any input.

Jeff
 
There's a better way to skin the cat...

In my search for an answer to the above post, I found a better way to merge Access data into a Word document.

Initially, I changed the above code to...
Code:
On Error Resume Next
With WordApp.Selection
.GoTo what:=wdGoToBookMark, Name:="Full Name"
If Err.Number = 0 Then .TypeText txtFullName.Value
[B]Err.Clear[/B]
.GoTo what:=wdGoToBookMark, Name:="DOB"
If Err.Number = 0 Then .TypeText DOB.Value
[B]Err.Clear[/B]
.GoTo what:=wdGoToBookMark, Name:="SSN"
If Err.Number = 0 Then .TypeText txtSSN.Value
[B]Err.Clear[/B]
End With
...with some success at avoiding an error #5099. Why this sort of works is beyond me, but it helped.

But my real breakthrough came with the following code:
Code:
On Error Resume Next
With WordApp.ActiveDocument
.Bookmarks("Full Name").Range.InsertAfter txtFullName.Value
.Bookmarks("DOB").Range.InsertAfter DOB.Value
.Bookmarks("SSN").Range.InsertAfter txtSSN.Value
End With
This is a superior way to mail merge because:
1. It doesn't generate error #5099 if a bookmark is not present.
2. It merges data into headers and footers without having to enter the header or footer programatically.
3. It can merge data into headers and footers that aren't even visible (Such as a non-first page header in a 1 page document)

Most (if not all) of the "Mail Merge" posts on the forum suggest the GoTo method but I think the Range.InsertAfter method is best. Hope this helps someone.

Jeff

P.S. Apologies for my sub-subject title to any PETA members out there.
 
Last edited:
Hi Jeff,

In this case you have to know if the bookmark exists. Why don't you use:
Code:
If WordApp.ActiveDocument.Bookmarks("DOB").exists Then
  WordApp.ActiveDocument.Bookmarks("DOB").Range.Text = DOB.Value
End If
That seems to me a better solution
 

Users who are viewing this thread

Back
Top Bottom