Open a Word Template from Access

G

Guest

Hello,
I am using Access 2003 with ADO. I have created a
template of a meeting invitation in MS Word with
bookmarks. From Access, I want to open the template,
save it to another .doc document and add the fields from
the Access form to the Word document. I am able to open
Word. What seems to be happening is that Word is
creating a new document but not based on the template.
Therefore, I get an error stating the bookmark is not
found. Below is a sample of the code:

Dim DPDDoc As Document
Dim WordAppl As Word.Application
Dim ClientFirst, ClientLast, Loc, Phone, Time As String
Dim MeetingDate As Date

Set WordAppl = New Word.Application
WordAppl.Visible = True
WordAppl.Documents.Open "C:\MeetingInvitation.dot"

WordAppl.Documents.Add
ClientFirst = Forms!MeetingForm!FirstName
ClientLast = Forms!MeetingForm!LastName
Loc = Forms!MeetingForm!MeetingLoc
Time = Forms!MeetingForm!MeetingTime

With WordAppl.ActiveDocument.Bookmarks
.Item("First").Range.Text = ClientFirst
.Item("Location").Range.Text = Loc
End With

Does anyone have any idea of what I am doing wrong?
Thanks so much in advance.
Debbie
 
A

Albert D. Kallal

I never did like those bookmarks.

You can give my sample word merge a try here:

It don't use book marks, so you will have to use those merge fields, but
then again, that is what most people are used to.

The other advantage of using merge fields is that you don't have to "hard"
code your field names into your code. You can imagine the dismay a company
would have when they are told that a programmer or developer has to be
brought in to change fields on a word merge document. (gee, you imagine if
you had to go to Microsoft each time you wanted change field in your word
merge?). For some cases, hard code fields is not a problem, but I have
encountered some clients that have been rather insulted, and suggested that
us developers were being dishonest by hard coding fields for a simple word
merge.

I think us developers are NOT trying to pull the wool over the eyes of
business when we hard code merge fields...but, some companies, and people
who use the merge software may very well think this way!

Anyway, as a result, I have a nice easy to use word merge library of code,
and it lets you use Merge Fields, and anyone can easily make a word merge
document. And, when making the word merge document, you get to use a "nice"
drop down list of merge fields in word.

So, if you still are struggling to get your example to work, you could cut
and paste your text into a word template (that my example software
creates),a and replace the books marks with actual merge fields.

You can find my example here:

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

Give the above example a try (it has some sample data). If the whole process
works on you pc, then I have instructions on how to use this in YOUR
application. The beauty is that only takes ONE line of code to word enable
any existing form you have..
 
K

Kevin K. Sullivan

PFJI,

You don't say which line of code causes the error -- that would help.

When I do stuff like this, I always like to keep explicit object variables
and not rely on ActiveDocument or anything like that.
My approach would be :
-------------------
Dim WordDoc As Word.Document
Dim WordAppl As Word.Application
'Dim ClientFirst, ClientLast, Loc, Phone, Time As String 'the first four
are Variants!
Dim ClientFirst As String, ClientLast As String, Loc As String, strTime as
String
Dim MeetingDate As Date
Dim strTemplate As String

Set WordAppl = New Word.Application
WordAppl.Visible = True
strTemplate = "C:\MeetingInvitation.dot"
Set WordDoc = WordAppl.Documents.Add(Template:=strTemplate)

ClientFirst = Forms!MeetingForm!FirstName
ClientLast = Forms!MeetingForm!LastName
Loc = Forms!MeetingForm!MeetingLoc
strTime = Forms!MeetingForm!MeetingTime

Debug.Print WordDoc.Bookmarks.Count 'show test presence of bookmarks

With WordDoc.Bookmarks
.Item("First").Range.Text = ClientFirst
.Item("Location").Range.Text = Loc
End With

WordDoc.Save 'Prompts user for directory, filename, etc.
'WordDoc.SaveAs "YourFileName.doc" ' Use this one if you know where you want
to save it

WordDoc.Close
Set WordDoc = Nothing ' release reference to document
WordAppl.Quit
Set WordAppl = Nothing ' release reference to application
--------------

A couple of potential traps:

1)
Dim ClientFirst, ClientLast, Loc, Phone, Time As String
This does not work as it would in many programming languages. The first
four variable are assumed to be variants, not strings. You need to type"
As String" after every variable name.

2)
Dim Time as String
Call this variable something else, like strTime. It's too easy to get
confused with the VBA function Time. If your not careful, you'll reset your
computer's clock!

HTH,

Kevin
 
J

John Nurick

Comments inline.

Hello,
I am using Access 2003 with ADO. I have created a
template of a meeting invitation in MS Word with
bookmarks. From Access, I want to open the template,
save it to another .doc document and add the fields from
the Access form to the Word document. I am able to open
Word. What seems to be happening is that Word is
creating a new document but not based on the template.
Therefore, I get an error stating the bookmark is not
found. Below is a sample of the code:

Dim DPDDoc As Document
Dim WordAppl As Word.Application

The next line declares ClientFirst, ClientLast, Loc and Phone as
Variants and Time as a String. This is probably not what you intend.
Dim ClientFirst, ClientLast, Loc, Phone, Time As String
Dim MeetingDate As Date

Set WordAppl = New Word.Application
WordAppl.Visible = True

The next line opens the specified template.
WordAppl.Documents.Open "C:\MeetingInvitation.dot"

The next line opens a new document based on Normal.dot
WordAppl.Documents.Add

If you want to create a new document based on your template, use
something like
Set DPDDoc = WordAppl.Documents.Add "C:\blah\blah.dot"
ClientFirst = Forms!MeetingForm!FirstName
ClientLast = Forms!MeetingForm!LastName
Loc = Forms!MeetingForm!MeetingLoc
Time = Forms!MeetingForm!MeetingTime

With WordAppl.ActiveDocument.Bookmarks
Safer to use
With DPDDoc.Bookmarks
 
G

Guest

Thank you all, I did get it to work with your help. Just for the future, I
had a terrible time finding help on the Word properties and methods. Any
idea where I could find information on them? Thanks again!
Debbie
 
J

John Nurick

Hi Debbie,

Very often the simplest way is with the Object Browser. Just hit F2 in
the VB Editor, make sure that the Word library is listed, and you can
then search for Word objects & properties just like Access ones. Once
you've found what you want, cross your fingers and hit F1: with luck
that will bring up some help.

For Word information generally, be sure to check out
http://word.mvps.org
 
G

Guest

John,
Thank you so much. Take care,
Debbie

John Nurick said:
Hi Debbie,

Very often the simplest way is with the Object Browser. Just hit F2 in
the VB Editor, make sure that the Word library is listed, and you can
then search for Word objects & properties just like Access ones. Once
you've found what you want, cross your fingers and hit F1: with luck
that will bring up some help.

For Word information generally, be sure to check out
http://word.mvps.org
 

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