ACC: How to Open and Close Word Documents with OLE Automation

Article translations Article translations
Article ID: 154456 - View products that this article applies to.
This article was previously published under Q154456
Expand all | Collapse all

On This Page

SUMMARY

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

This Article demonstrates how to use OLE Automation to Open and Close Microsoft Word for Windows 95 documents.

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.

NOTE: Visual Basic for Applications (used in Microsoft Access for Windows 95 version 7.0) is called Access Basic in version 2.0. For more information about Access Basic, please refer to the "Building Applications" manual.

For more information about using OLE Automation between Microsoft Access and Microsoft Word, search on the phrase Create a Word Document using OLE Automation, and then CreateObject function using the Microsoft Access for Windows 95 Answer Wizard. For more information about the Microsoft Access Verb and Action properties, search on the phrases Verb property and Action property, using the Microsoft Access Help Index.

MORE INFORMATION

The following two examples demonstrate how to manipulate Microsoft Word 7.0 documents:

Example 1

The following example assumes there is a document named Test.doc in the C:\Winword folder (directory). This example demonstrates how to open a Microsoft Word Document, reformat the text, and then close the document:

  1. Open Microsoft Access and open the sample database Northwind.mdb. (NWIND.MDB in Microsoft Access version 2.0)
  2. Create a new module and add the following function:
          Function CloseDoc ()
            Dim wordobj As Object
            Set wordobj = CreateObject("Word.Basic")
            wordobj.FileOpen "C:\Winword\Test.doc"
            ' View the invoked instance of Word.
            wordobj.AppShow
            ' Selects the entire document and makes it Bold.
            wordobj.EditSelectAll
            wordobj.Bold 1
            ' Closes the document and saves changes.
            wordobj.filesave
            wordobj.FileClose (1)
            wordobj.appclose
            Set wordobj = Nothing
          End Function
    						
  3. Type the following into the Debug window (or Immediate window in Microsoft Access version 2.0), and then press ENTER:
          ?Closedoc()
    						
    Note that the object is opened, formatted, saved, and then closed.

Example 2

The following example demonstrates how to close a Microsoft Word document which is embedded on a form:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 2.0) and create a new form.
  2. In Design view, create an unbound object frame named OLEObj on the form. Change the Enabled property for the object frame control to Yes and the Locked property to No. When prompted with the Insert Object dialog box, click "Create New" and in the list of Object Types click "Microsoft Word Document." Click OK. In the blank Word document that appears, enter some text, and then on the File menu, click "Close and Return to Formx: Form."
  3. Add a command button to the form and for the OnClick property add the following code:
          Dim WordObj as Object
          Me![OLEObj].Verb = -2   ' Tells Microsoft Access to open the
                                  ' application.
          Me![OLEObj].Action = 7  ' Activates the application.
          Set WordObj = Me![OLEObj].Object.Application.WordBasic
          WordObj.EditSelectAll
          WordObj.Bold 1
          Wordobj.FileSave
          WordObj.FileClose(1)
          Wordobj.Appclose
          Set WordObj = Nothing
    						
  4. Open the form and click the command button. Note that OLE Object is opened, formatted, and then closed.

Properties

Article ID: 154456 - Last Review: October 11, 2006 - Revision: 1.2
APPLIES TO
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Word 6.0c
Keywords: 
kbhowto kbinterop kbprogramming KB154456
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com