Exporting Data to Word

M

Meryl

I have an event procedure to create a mailing address in
Word from my form using bookmarks in Word. The mailing
address is for doctors, where not all the fields may have
values. For instance, some doctors work at a hospital
and others don't, some have a floor or suite #, other's
don't.

If a field has a null value, then I get a "type mismatch"
error message and none of the remaining bookmarks are
filled in in Word. Is there some way to code for this?
Below is my current code. Thank you for your help!

Private Sub AddressLabelProf_Click()
On Error GoTo Err_AddressLabelProf_Click

Dim oApp As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

oApp.Documents.Add "C:\Windows\Application
Data\Microsoft\Templates\AddressLabelProf.dot"
oApp.Visible = True

' The following code is generated from your Access form
while it is
' manipulating an instance of Word.
' So, while it looks somewhat like VBA, it's really Word
VBA.

If oApp.ActiveDocument.Bookmarks.Exists("FirstName") =
True Then
oApp.ActiveDocument.Bookmarks("FirstName").Range.Text
= Me![First Name]
End If

If oApp.ActiveDocument.Bookmarks.Exists("LastName") =
True Then
oApp.ActiveDocument.Bookmarks("LastName").Range.Text
= Me![Last Name]
End If

If oApp.ActiveDocument.Bookmarks.Exists("Address1") =
True Then
oApp.ActiveDocument.Bookmarks("Address1").Range.Text
= Me![Mailing Address Line 1]
End If

If oApp.ActiveDocument.Bookmarks.Exists("Floor") = True
Then
oApp.ActiveDocument.Bookmarks("Floor").Range.Text =
Me![Suite/Floor]
End If

If oApp.ActiveDocument.Bookmarks.Exists("Address2") =
True Then
oApp.ActiveDocument.Bookmarks("Address2").Range.Text
= Me![Mailing Address Line 2]
End If

If oApp.ActiveDocument.Bookmarks.Exists("City") = True
Then
oApp.ActiveDocument.Bookmarks("City").Range.Text = Me!
City
End If

If oApp.ActiveDocument.Bookmarks.Exists("State") = True
Then
oApp.ActiveDocument.Bookmarks("State").Range.Text =
Me![State Code]
End If

If oApp.ActiveDocument.Bookmarks.Exists("Zip") = True
Then
oApp.ActiveDocument.Bookmarks("Zip").Range.Text = Me!
[Postal Code]
End If

Exit_AddressLabelProf_Click:
Exit Sub
..
 
D

Dave Jones

Meryl,
Check to see if the field is null before you try and pass it to the
bookmark. For Example:

If oApp.ActiveDocument.Bookmarks.Exists("FirstName") = True Then
If not Isnull Me![FirstName] Then
oApp.ActiveDocument.Bookmarks("FirstName").Range.Text = Me![First Name]
End If


Dave
 
A

Albert D. Kallal

I would suggest you dump the use of all that code and bookmarks, and go back
to mail merge fields. They are MUCH easer to use, and for each new field you
add, you don't have to change your code (can you imagine a word processor
that requires you to change code for each new letter?).

Try given the following word merge sample a try. It will fix all of your
problems, and good number more. Furhter, no hard coding of merge fields is
requited for each new merge. in fact, for each new letter template, NO new
code is required.

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

Tim Ferguson

I would suggest you dump the use of all that code and bookmarks, and
go back to mail merge fields. They are MUCH easer to use,

And of course, using an Access report is even easier again. And it will run
much quicker.

Tim F
 
D

DavidL

Yes, but Access Reports have limitations like not allowing
the receipt to insert text/numbers/graphics into
SnapShots. And if you are working with a text-centric
report, exporting them to Word RTF is a real pain.

Personally, I would like for Redmond to move closer to the
functionality of Word.

My 2 cents.
 
T

Tim Ferguson

Yes, but Access Reports have limitations like not allowing
the receipt to insert text/numbers/graphics into
SnapShots. And if you are working with a text-centric
report, exporting them to Word RTF is a real pain.

This may be application-specific, but most of my reports to straight to the
printer. If it is going to end up as a disk file, then I usually do the
whole lot by hand using Print # commands. I guess that someone, somewhere,
has found a use for RTF files, but I just don't know what it is.
Personally, I would like for Redmond to move closer to the
functionality of Word.

Why, when Word has practically all the functionality of Access? There is
DAO and ADO and a pretty GUI and all-the-fonts-you-could-want. Obviously,
MSForms do not cut it against Access forms, but we are talking about paper
output here, not data entry; and they are absolutely fine for entering
parameters for forms. I still reckon, though, that most form letters,
sticky labels, line-by-line reports would be quicker to set up as Access
reports.

YMMV of course.


Tim F
 

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