Microsoft KB Archive/159328

= ACC: How to Use Automation to Run Word 97 Mail Merge from Access =

Article ID: 159328

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q159328





SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to use Automation to run a Microsoft Word 97 for Windows mail merge in Microsoft Access version 7.0 or 97.

For information about how to run a Word 7.0 mail merge in Microsoft Access 7.0, please see the following article here in the Microsoft Knowledge Base:

154571 ACC95: Running Word Mail Merge from Access Using OLE Automation

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
Word 97 takes advantage of the Visual Basic for Applications object hierarchy used in other Microsoft Office applications. Word 7.0 and earlier versions used a flat command model called WordBasic.

By using Automation, you can perform a mail merge in a Microsoft Word 97 document from Microsoft Access. The example in this article uses the OpenDataSource and Execute methods of the MailMerge object in Word 97.

Example: Mail Merge a Microsoft Access Query with a Word 97 Document
The following example opens a Word 97 document called C:\MyMerge.doc and runs a mail merge by using the Customers table in the Microsoft Access sample database Northwind.mdb as its data source. The following sample code assumes that the main document for the merge, C:\MyMerge.doc, already exists.

 Start Microsoft Access and open any database, or create a new one.  Create a module and type the following procedure: Function MergeIt Dim objWord As Word.Document Set objWord = GetObject("C:\MyMerge.doc", "Word.Document") ' Make Word visible. objWord.Application.Visible = True ' Set the mail merge data source as the Northwind database. objWord.MailMerge.OpenDataSource _ Name:="C:\Program Files\Microsoft " & _ "Office\Office\Samples\Northwind.mdb", _ LinkToSource:=True, _ Connection:="TABLE Customers", _ SQLStatement:="Select * from [Customers]" ' Execute the mail merge. objWord.MailMerge.Execute End Function NOTE: If you want to print the merged document, delete the Execute statement above and add the following four lines of code above the End Function statement: objWord.MailMerge.Destination = wdSendToNewDocument objWord.MailMerge.Execute

'The following line must follow the Execute statement because the 'PrintBackground property is available only when a document window is  'active. Without this line of code, the function will end before Word 'can print the merged document.

objWord.Application.Options.PrintBackground = False objWord.Application.ActiveDocument.PrintOut  With the module still open in Design view, click References on the Tools menu. Add the Word 97 Object Library to the list of available references. If the Object Library is not on the list, click the Browse button and locate the file Msword8.olb. To test this function, type the following line in the Debug window, and then press ENTER:

?MergeIt

An instance of Word 97 opens, displays MyMerge.doc, and then merges it with the Customers table in the sample database Northwind.mdb.

