Microsoft KB Archive/209976

= How to use automation to run Word 2000 mail merge from Access 2000 =

Article ID: 209976

Article Last Modified on 8/6/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition
 * Microsoft Word 2000 Standard Edition

-



This article was previously published under Q209976





For a Microsoft Access 97 version of this article, see 159328.

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

This article applies only to a Microsoft Access database (.mdb).



SUMMARY
This article shows you how to use Visual Basic for Applications to run a Microsoft Word mail merge in Microsoft Access.



MORE INFORMATION
By using Visual Basic for Applications, you can perform a mail merge in a Microsoft Word document from a Microsoft Access module. The example in this article uses the OpenDataSource and Execute methods of the MailMerge object in Word. It also demonstrates how to send a mail merge document directly to the printer.

Example: Mail Merge a Microsoft Access Query with a Word Document
The following example opens a Word 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 9 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 Msword9.olb. To test this function, type the following line in the Immediate window, and then press ENTER:

?MergeIt

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

