This article was previously published under Q209976
This article has been archived. It is offered "as is" and will no longer be updated.
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).
This article shows you how to use Visual Basic for Applications to run a Microsoft Word mail merge in Microsoft Access.
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.ExecuteEnd 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 = wdSendToNewDocumentobjWord.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 = FalseobjWord.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:
An instance of Word opens, displays MyMerge.doc, and then merges it with the Customers table in the sample database Northwind.mdb.
For more information about creating mail merge documents, click Microsoft Word Help on the Help menu, type use mail merge to create form letters in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.
For more information about using data from Microsoft Access tables or queries in mail merge, click Microsoft Access Help on the Help menu, type merge data from a table or query by using the microsoft word mail merge wizard in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.