This article applies only to a Microsoft Access database (.mdb).
Example: Mail Merge a Microsoft Access Query with a Word DocumentThe 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: 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:
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.
Name:="C:\Program Files\Microsoft " & _
Connection:="TABLE Customers", _
SQLStatement:="SELECT * FROM [Customers]"
' Execute the mail merge.
objWord.MailMerge.Destination = wdSendToNewDocument
'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
- 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.
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.
Article ID: 209976 - Last Review: Aug 6, 2004 - Revision: 1