Word 2007 mailmerge from Access 2007

J

jim

We have created a mail merge document, "Deposit Letter.docx" created in Word
2007, whereas the datasource is a paramenter query created in Access 2007,
which is connected by a DDE connection. The word document can be opened from
inside Access by selecting a button that has the command attached. The
problem we have is when the document is selcted to be opened, it does not
retrieve the data created by the query. It opens but will only show one
page(the document will have multiple pages), that has old data inserted. To
have the document show the current data from the query, the mail merge wizard
has to be started and that whole process has to be completed each time. It
seems to not keep the connection between the two applications.

The goal is when the document is opened, the data from the query is inserted
without having to go through the whole mail merge process. Thanks for any
assistance.
 
J

jim

I did change the registry key per the help document, but now I get a run-time
error 5640. Debugging the error, it highlights oApp.Documents.Open
FileName:=LWordDoc in the module shown below.

Private Sub Command0_Click()


Dim LWordDoc As String
Dim oApp As Object

'Path to the word document
LWordDoc = "I:\Deposit Letter.docx"

If Dir(LWordDoc) = "" Then
MsgBox "Document not found."

Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True

'Open the Document
oApp.Documents.Open FileName:=LWordDoc
End If

Thanks for the help.
 
P

Peter Jamieson

I suggest you try creating the mail merge link in VBA once you have opened
the document.

e.g., ensure that the document is not a mail merge main document (you will
lose any sort/filter stuff, but all the fields should remain), then in your
code, use something like

Dim oDoc As Object 'or Word.Document if you want early binding
Set oDoc = oApp.Documents.Open FileName:=LWordDoc
oDoc.Mailmerge.OpenDataSource _
Name:="the full path name of your database", _
.Connection:="QUERY [the query name]", _
.SQLStatement:="SELECT * FROM [the query name]", _
.Subtype:=wdMergeSubtypeWord2000


' then make sure othe oDoc.MailMerge properties such as mail merge type and
destination are set the way you want.

' eventually, set oDoc = Nothing

This is on-the-fly so I can't be sure that the parameters for OpenDataSource
are correct in this case. You'll need this Subtype for Word 2002 and later
to get a DDE connection, but you may be able to ditch the SQLStatement and
the Connection may need to be different. You can inspect values for an
existing DDE-connected document using VBA.

Alternatively, you may find Albert Kallal's stuff at (I think)

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

a more reliable way to do this.
 

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