Microsoft KB Archive/159328

From BetaArchive Wiki

Article ID: 159328

Article Last Modified on 1/19/2007



APPLIES TO

  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Word 97 Standard Edition



This article was previously published under Q159328


SUMMARY

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

This article shows you 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:

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

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

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


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: kbhowto kbinterop kbprogramming kbualink97 KB159328