Merge All Records Generated by a Query to Word Template

J

Julia

I have created a from with a button. In the on click event, there has
the following code to generate the current record to a word template.
The form has also linked to a query as the data source. May I know how
to edit the code in order to export all the records from the query to
the word template?

thank, julia

********************************************************************
Dim objWord As Object
' Open Microsoft Word using automation
Set objWord = New Word.Application
objWord.Documents.Add "c:\FormLetter.dot"
objWord.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 objWord.ActiveDocument.Bookmarks.Exists("FirstName") = True Then
objWord.ActiveDocument.Bookmarks("FirstName").Range.Text =
me!FirstName
End If
If objWord.ActiveDocument.Bookmarks.Exists("LastName") = True Then
objWord.ActiveDocument.Bookmarks("LastName").Range.Text =
me!LastName
End If
 
L

Larry Daugherty

Hi Julis,

While you didn't ask it in these terms, your post really requests that
the responder icomplete your higher level design and then implement it
for you. You're moving in an interesting direction and one in which I
intend going before long as I extend and enhance an application. I
expect that addressing the right issues in the right order will be
more work than I'd want to do for a casual newsgroup post. The
general rule is that you post as specific an issue as possible to
receive an answer on that issue alone. If you ask a broad or general
question expect to receive a broad or general answer, like this one.

Assuming you want one name per document: You'll need a loop that
contains both Word and Access code. Only open Word once and leave it
open until you've finished the last document. You'll have to get the
recordset from the query and write a new Word document and close it
and then open another document so long as there are unprocessed
records in the query.

Your higher level design will determine the sode you need.

HTH
 
A

Albert D.Kallal

I going to suggest that you dump those book marks.

The are hard to read, hard to use, generally send people to a room with
padded walls.

Worse, is that for EACH NEW letter, or template your make, you are required
to write new code. Can you imagine if you needed to write code for each new
letter you write with word, and had to be back to the original developers of
word? That would be nuts, and worse, many companies I consult for would
consider is DISHONEST on my part if I gave them some software that required
new code be written for each merge letter that I made. (great idea, make
money off of each new letter that a company writes since they have to bring
in the developer!!).

A much better solution is to use words built-in merge ability.

I have a working sample that allows you to send the current record you are
viewing to a word as a merge, or you can supply the merge a query.

Download, and try my word merge here:
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

Note on the sample form there is two buttons. One button sends the current
record to the merge, and the other can be used to send a query to the merge.
In both cases, you only need WRITE ONE LINE OF CODE to do this, and you get
to use a merge document.

Further, your word documents get to use a nice "drop down" list of fields
via the standard word merge system.
 

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