Blank Field merge error

  • Thread starter Thread starter James T.
  • Start date Start date
J

James T.

Using Access 2002

I have used this code before, and it always worked. Now
for some reason it is not working. I am using it to merge
a record to a word doc that has bookmarks in it. If the
record field is empty, then it should bypass that bookmark
and go on to the next bookmark. When I run the merge, I am
getting an error and in looking at the code the error is
the first field without data.

The code at the botton where it tells it to put in "" if
there is no data is where it is hanging up on me.

Any help or suggestions appreciated.

Example:

Private Sub Command112_Click()
' start Microsoft Word.
Set wordApp = CreateObject("Word.Application")

With wordApp

' Make the application visible.
.Visible = True
' Open the document.
.Documents.Open ("path to word.doc")
' Move to each bookmark and insert text from the
form.
.ActiveDocument.Bookmarks("NamePg1").Select
.Selection.Text = (CStr(Forms!ApplicationInputForm!
NamePg1))
...etc...

End With

'''objWord.ActiveDocument.PrintPreview
wordApp.ActiveDocument.PrintOut Background:=False
wordApp.ActiveDocument.Close wdDoNotSaveChanges

wordApp.Quit wdDoNotSaveChanges
Set wordApp = Nothing

Exit Sub

Mergebuttonbutton_Err:

' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
wordApp.Selection.Text = ""
Resume Next
End If

End Sub
 
You need to check if the field value is null, and then NOT send a value.
Perhaps a better way would be to use:

nz(recordset!LastName,"")

In the above, if the last is null, then a blank, or empty string is retuned.

on the other hand, why bother with all those book marks? They are hard to
read, hard to use, hard to insert and maintain in a document. Why not use
the mail merge fields? They are SOOOO much easier, and you don't have to
write a bunch of code to "insert" each data field. I mean, every time you
have a new screen or new table or anything, you have to write reams of code
to make the merge happen. Using words built-in merge feature means that you
don't have to hard code the field names in your code, and further don't have
to "guess", or make sure that the field names you use in the word doc are
EXACTLY the same. If you use the word merge feature, then you get a nice
drop down list of fields in the word document.

Unless you have some special formatting, or processing that you do..I would
dump those book marks.

I have a sample working solution that is based on the word merge, and you
don't have to write a bunch of code for EACH form or different word document
since I use the built in merge fields. Check out the working sample at:

http://www.attcanada.net/~kallal.msn/msaccess/msaccess.html

The above lets you merge one record (the one you are looking at in the
form), or merge many records documents to a single document (both of which
require a lot more work when using bookmarks).
 
Back
Top