ACC: Using Automation to Run Word 97 Mail Merge from MS Access

Last reviewed: November 21, 1997
Article ID: Q159328
The information in this article applies to:
  • Microsoft Access, versions 7.0, 97
  • Microsoft Word 97 for Windows

SUMMARY

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

This article demonstrates 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:

   ARTICLE-ID: Q154571
   TITLE     : 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.

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 executes a mail merge 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.

  1. Start Microsoft Access and open any database, or create a new one.

  2. 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 would like 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
    
    

  3. 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.

  4. 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 Northwind sample database.

REFERENCES

For more information about Automation, search the Help Index for "Automation," or ask the Microsoft Access 97 Office Assistant.

For information about using the Microsoft Access Object Browser to explore the methods and properties of Microsoft Word 97 Visual Basic for Applications objects, search the Help Index for "Automation, using the Object Browser," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbinterop kbualink97 IntpOleA
Technology        : kbole
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.