Launching Word Mail Merge from Access VBA

S

SRobertson2

I have a Word 2002 document that is linked to an ODBC data source (an Access
table). I want to open this doucment (with merged data) from a button in
Access.

The following code fails because the document never opens in a
"wdMainAndDataSource" state. What am I doing wrong?

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wMM As Word.MailMerge

Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Open("C:\Donating.doc")
Set wMM = ActiveDocument.MailMerge
If wMM.State = wdMainAndDataSource Then
wMM.Destination = wdSendToNewDocument
wMM.Execute
End If
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?U1JvYmVydHNvbjI=?=,
I have a Word 2002 document that is linked to an ODBC data source (an Access
table). I want to open this doucment (with merged data) from a button in
Access.

The following code fails because the document never opens in a
"wdMainAndDataSource" state. What am I doing wrong?
It's not what you're doing wrong, it's a security measure in Word 2002 and
later. See this KB article; you need to either add a Registry entry or use your
code to link in the data source

"Opening This Will Run the Following SQL Command" Message When You Open a Word
Document - 825765
http://support.microsoft.com?kbid=825765
Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim wMM As Word.MailMerge

Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Open("C:\Donating.doc")
Set wMM = ActiveDocument.MailMerge
If wMM.State = wdMainAndDataSource Then
wMM.Destination = wdSendToNewDocument
wMM.Execute
End If

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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