Microsoft KB Archive/154571

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

Article ID: 154571

Article Last Modified on 10/11/2006

-

APPLIES TO


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

-



This article was previously published under Q154571



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
This article demonstrates how to use OLE Automation to run a Microsoft Word for Windows 95, version 7.0 mail merge.

Microsoft Word 97 uses the Visual Basic for Applications object model for Automation code. For an example of how to run a mail merge using Microsoft Access 7.0 or 97 with Microsoft Word 97, please see the following article in the Microsoft Knowledge Base:

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

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 the "Building Applications with Microsoft Access for Windows 95" manual.



MORE INFORMATION
Through OLE Automation, it is possible to set up and perform a mail merge in a Microsoft Word document using OLE Automation in Microsoft Access. This example uses the WordBasic MailMergeOpenDataSource and MailMerge statements.

Example: Mail Merge a Microsoft Access Query with a Word Document
The following example opens a Word document called Mymerge.doc and through an OLE object variable, runs the WordBasic MailMergeOpenDataSource statement, which turns the opened document into the main document for the mail merge process. The following code example assumes that a document, C:\Mymerge.doc, exists.

To run this code:  Open Microsoft Word. When the code is run from Microsoft Access, Word remains open. Open a Microsoft Access for Windows 95 database and create a new module.  Type the following function in the Module window: Function MergeIt Dim ThisWord As Word95ACC.Word95Access Set ThisWord = CreateObject("Word.Basic") ThisWord.FileOpen Name:="C:\MYMERGE.DOC" ThisWord.AppShow With ThisWord .MailMergeOpenDataSource _ Name:="C:\MSOFFICE\ACCESS\Samples\Northwind.mdb", _ LinkToSource:=1, _ Connection:="TABLE Customers", _ SQLStatement:="SELECT * FROM [Customers]" .MailMerge CheckErrors:=1, Destination:=0, MergeRecords:=1, _ From:="1", To:="10", MailMerge:=1 End With End Function  On the Tools menu, click References. Activate the Word 95 Objects for ACCESS from the Available References list. If it does not exist in the list, click the Browse button and locate the file Wd95acc.tlb (this file usually exists in the \Access folder). To run the function, open the Debug window, type the following line, and then press ENTER:

? MergeIt



NOTE: When the OLE Automation object goes out of scope, the instance of Microsoft Word is unloaded, unless the object was created from a previous instance (already opened).

<div class="references_section">