ACC: How to Use Automation to Print a Microsoft Word Document

ID: Q154569

The information in this article applies to:

SUMMARY

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

This article shows you how to use Automation to print a Microsoft Word document that you are working with in a Microsoft Access function or in a Microsoft Access form's object frame.

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

When you use Microsoft Access with Microsoft Word 7.0, you must use Word Basic commands in all Automation code. Microsoft Word 97 uses Visual Basic for Applications. The following examples show syntax for both. Each example assumes you have a Microsoft Word document called C:\Wordtest.doc.

Example - Printing a Microsoft Word Document

This example opens and prints a Microsoft Word document.

1. Start Microsoft Access and open any database.

2. Create a module and type the following procedure:

   For Microsoft Word 97:

      Function PrintDoc()
      Dim WordObj As Object
      Set WordObj = CreateObject("Word.Application")
      WordObj.Documents.Open "C:\Wordtest.doc"
      WordObj.PrintOut Background:=False
      WordObj.Quit
      Set WordObj = Nothing
      End Function

   For Microsoft Word 7.0:

      Function PrintDoc()
      Dim WordObj As Object
      Set WordObj = CreateObject("Word.Basic")
      WordObj.FileOpen "C:\Wordtest.doc"
      ' Where 2 is the number of copies. In this example, the parameter
      ' names have been included to show which argument does what.
      WordObj.FilePrint Background:=0, AppendPrFile:=0, Range:=0, _
      PrToFileName:="", From:="", To:="", Type:=0, NumCopies:=2
      ' The following line demonstrates how to preview instead of print
      ' WordObj.FilePrintPreview.
      ' Close without saving.
      WordObj.FileClose 2
      Set WordObj = Nothing
      End Function

3. To test this function, type the following line in the Debug window,
   and then press ENTER.

      ? PrintDoc()

Example - Printing a Microsoft Word Document in a Form's Object Frame

This example shows how to print an embedded Microsoft Word object on a form.

1. Start Microsoft Access and open any database.

2. Create a new form not based on any table or query in Design view.

3. Add an unbound object frame control to the form.

4. In the Insert Object dialog box, click Create From File and type

   C:\Wordtest.doc in the File box. Click OK.

5. Set the Name property of the object frame to OLEObj.

6. Add a command button to the form and set the following properties:

      Command Button:
         Name: PrintDoc
         Caption: Print Word Doc
         OnClick: [Event Procedure]

7. Click the Build button next to the command button's OnClick property and
   type the following procedure:

   For Microsoft Word 97:

      Private Sub PrintDoc_Click()
      Dim WordObj As Object
      Me![OLEObj].Verb = -2   'Tells Access to open the application
      Me![OLEObj].Action = 7  'Activates the application
      Set WordObj = Me![OLEObj].Object.Application
      WordObj.PrintOut Background:=False
      WordObj.Quit
      Set WordObj = Nothing
      End Sub

   For Microsoft Word 7.0:

      Private Sub PrintDoc_Click()
      Dim WordObj As Object
      Me![OLEObj].Verb = -2   'Tells Access to open the application
      Me![OLEObj].Action = 7  'Activates the application
      Set WordObj = Me![OLEObj].Object.Application.WordBasic
      ' This example prints the current document using FilePrintDefault.
      WordObj.FilePrintDefault
      ' The following line demonstrates how to preview instead of print.
      ' Comment out the FileClose method that follows, in order to leave
      ' Print Preview open.
      ' WordObj.FilePrintPreview
      WordObj.FileClose 2
      End Sub

8. Open the form in Form view and click the Print Word Doc button. Note
   that Microsoft Word starts, prints the document, and then returns to
   the form.

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

REFERENCES

For more information about using Automation, search the Help Index for "Automation."

For more information about the Verb and Action properties, search the Help Index for "Verb property" or "Action property."

Additional query words:

Keywords          : kbinterop IntpOlea 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998