Fill Word template with Access data

M

michirure

I have the following codes which open a word template and fill it with data
from current record in Access.

Set oApp = CreateObject("Word.Application")
WordTemplate = "D:\Temp\Merge Letter.doc"

With oApp
.Visible = True
.Documents.Add (WordTemplate)

.ActiveDocument.Bookmarks("Account").Select
.Selection.Text = (CStr(Me.[Account]))
.ActiveDocument.Bookmarks("Acct_Phone").Select
.Selection.Text = (CStr(Me.[Acct_Phone]))
.Activate
End With

The codes work well, but now another problem comes up. In my access form,
some fileds are blank because I don't have certain info about the client eg.
phone number.

When I tried to use the If...then...else function, I got the error
msg "Object required"

.ActiveDocument.Bookmarks("Acct_Phone").Select
If Me.[Acct_Phone] Is Null Then
.Selection.Text = ""
Else
.Selection.Text = (CStr(Me.[Acct_Phone]))
End If

Please help. Thanks
 
A

Albert D. Kallal

, do you really want to use book marks?

Bookmarks are hard to see, and when you place them into he document you
actually have to be sure you typed the correct book mark name.
(and, you have to remember the exact spelling of the field name).

Worse, is every you need a new template, you have to write new code. Can you
image if every time you need a new letter in word, you had to go back to the
vendor..and have them modify the code? (if my clients found me doing
this..they would consider this a conflict of interest).

If you use words built in merge ability, the end users who been trained in
using the merge feature of word will not need re-training. Further, they
don't ever have to "type" the field name(s) into the document as book
marks - the users get to use a "nice" dropdown list of availing merge
fields, and can insert them as standard merge fields.


I have easy word merge system in which is merges the current forms record
by default. further, the merge system works for any form you make..and does
NOT require you to write code for each new document, or form you add to your
application. And, since it uses the built in word merge, the if the field is
empty...no problems....

You can find my super easy word merge here:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

It is not huge you use bookmarks, but I do advoid them if I don't need them,
and further, for each new form/table, you have to write new code, and if you
use words built in "merge", then you advoid coding each time. And, futher,
you can take existing word merge templates, and use them.

However, there are some reasons for using books marks.

You existing code can be fixed:

If isNull(Me.[Acct_Phone]) = True then

So, you have to use the isNull() function in code.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top