Controlling Word mailmerge from Access VBA

E

Evahn Derkley

Hi

I'm having a problem with setting up a Word 2000 mailmerge
document to use an Access query for the data source and
running this all from an Access 2000 VBA module.

What happens is that the VBA code opens the Word template
document and when it runs the MailMerge.OpenDataSource
method, Word opens a second copy of the database passed
using the "Name" parameter and tries to use this as it's
data source instead of the already open database.

This does work if the mailmerge data source is to be a
Table, however it does open a new copy of the database
everytime the statement is run.

If a query is passed as the mailmerge data source and the
query has references to fields on an open form, the
mailmerge then fails with numerous errors. This seems to
be because the query is looking for fields on a form on
the newly opened copy of the database and doesn't find
them.

How can I stop Word from opening this second copy of the
database and use the running copy instead?

The Access database does not have any security on it and
I'm using Microsoft Office 2000 with SR-1 and SP-2
installed. It does the same behaviour on workstations
with Access 2000 Run-time installed.


Tidied up code from Access VBA module:

Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim strSQL As String

Set WordObj = CreateObject("Word.Application")
WordObj.Visible = True
Set WordDoc = .Documents.Open(FileName:="c:\template.doc",
ReadOnly:=True)
strSQL = "Select * from [qMailMergeSource]"
WordDoc.MailMerge.OpenDataSource Name:=CurrentDb.Name,
LinkToSource:=True, Revert:=True, Connection:="QUERY
qMailMergeSource", SQLStatement:=strSQL
WordDoc.MailMerge.Destination = wdSendToNewDocument
WordDoc.MailMerge.Execute


NB: I've tried opening the Word document as non-Readonly
and I've also tried different combinations of the
LinkToSource and Revert fields.


Thanks
Evahn
 
A

Albert D. Kallal

The launching of the 2nd copy is a know bug. There is a workaround at

http://www.mvps.org/access/bugs/bugs0011.htm

However , I a have a battle tested and ready to go word merge example that
eliminates the above bug, and MANY other problems. I suggest you download my
example, and give it a try.

It is designed to word template enable any *existing* form you have with ONE
line of code. Give it a try, and read the notes on how I made the merge
reliable.

Check out:

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

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