Automating Word mail merge w/ VB 6

J

Jonathan

Hello all,

I'm trying to automate an MS-Word 2002 mail merge operation using an Access
2002 database as the data source. I created a Word document with the MM
fields already set up. The MM fields are mapped to the database.



I have a VB 6.0 program that populates the records in the database and then
opens up the word document. Word launches and the specified document opens
but the merge does not execute. The MM fields are there but they aren't
populated. Here's the code. Any help would be appreciated.



-Jonathan





Private Sub OpenDoc(Index As Integer)

Err.Number = 0

On Error GoTo NotLoaded

Set appMSWord = GetObject(, "Word.Application")

NotLoaded:

Select Case Err.Number

Case 429

Set appMSWord = CreateObject("Word.Application")

Case 0

MsgBox "Close all open MS-Word documents.", vbOKOnly, gsTitle

Exit Sub

Case Else

ErrorHandler Err.Number, Err.Description, "OpenDoc()"

Exit Sub

End Select



On Error GoTo opendoc_EH



appMSWord.Visible = True

Select Case Index

Case 0

appMSWord.Documents.Open "doc1.doc"

Case 1

appMSWord.Documents.Open "doc2.doc"

Case 2

appMSWord.Documents.Open "doc3.doc"

Case 3

appMSWord.Documents.Open "doc4.doc"

Case 4

appMSWord.Documents.Open "doc5.doc"

Case 5

appMSWord.Documents.Open "doc6.doc"

Case Else

MsgBox "Invalid document specified.", vbCritical, gsTitle

End Select

OpenDoc_Exit:

Exit Sub



opendoc_EH:

ErrorHandler Err.Number, Err.Description, "OpenDoc()"

Resume OpenDoc_Exit



End Sub
 
K

Ken Halter

Jonathan said:
Hello all,

I'm trying to automate an MS-Word 2002 mail merge operation using an
Access
2002 database as the data source. I created a Word document with the MM
fields already set up. The MM fields are mapped to the database.



I have a VB 6.0 program that populates the records in the database and
then
opens up the word document. Word launches and the specified document opens
but the merge does not execute. The MM fields are there but they aren't
populated. Here's the code. Any help would be appreciated.



-Jonathan

There are several 'Mail Merge' documents in this list....

Search The Knowledge Base
http://support.microsoft.com/search...&kt=ALL&mdt=0&comm=1&ast=1&ast=2&ast=3&mode=a
 
M

MikeD

Jonathan said:
Hello all,

I'm trying to automate an MS-Word 2002 mail merge operation using an
Access
2002 database as the data source. I created a Word document with the MM
fields already set up. The MM fields are mapped to the database.

I have a VB 6.0 program that populates the records in the database and
then
opens up the word document. Word launches and the specified document opens
but the merge does not execute. The MM fields are there but they aren't
populated. Here's the code. Any help would be appreciated.

See if any of the following help:

How to automate Word with Visual Basic to create a Mail Merge
http://support.microsoft.com/kb/285332/en-us

How To Automate Word from Visual Basic to Create a Mail Merge for Mailing
Labels
http://support.microsoft.com/kb/258512/en-us

How To Automate Microsoft Word to Perform Mail Merge from Visual Basic
http://support.microsoft.com/kb/220607/en-us

OFFAUTMN.exe discusses Office 97 and 2000 Automation and provides sample
code
http://support.microsoft.com/kb/253235/en-us

PRB: Prompt to Select Table with Word 2002 Mail Merge Code for Excel or
Access Data Source
http://support.microsoft.com/kb/289830/EN-US/

If you search the MS Knowledge Base, you'll find many more articles about
this.
 
C

Cindy M -WordMVP-

Hi Jonathan,
I'm trying to automate an MS-Word 2002 mail merge operation using an Access
2002 database as the data source. I created a Word document with the MM
fields already set up. The MM fields are mapped to the database.
1. When you open this document manually, do you see a prompt about SQL that's
going to execute?

2. If you do, put an "IF" in your code to check the MainDocumentType. Has it
been reverted to a "Normal" Word document?

If yes, check out this article:
"Opening This Will Run the Following SQL Command" Message When You Open a
Word Document - 825765
http://support.microsoft.com?kbid=825765

Either the Registry option mentioned here has to be set, or your code needs
to link in the data source explicitly.

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