ACC95: Using MS Word's FileClose/DocClose Statements in OLE

Last reviewed: August 29, 1997
Article ID: Q156226
The information in this article applies to:
  • Microsoft Access version 7.0

SYMPTOMS

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

The WordBasic FileClose and DocClose statements unexpectedly close Microsoft Word when used through OLE automation.

This article assumes that you are familiar with Visual Basic for Applications, Word Basic 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.

CAUSE

The FileClose and DocClose statements close Microsoft Word unexpectedly if the following conditions are true:

  • Microsoft Word is started through OLE automation. In other words, Microsoft Word is not previously running when the OLE Automation session begins.
  • The FileClose or DocClose statement is used via OLE Automation from any OLE Automation client.
  • Microsoft Word is not visible during the operation.

RESOLUTION

Use either of the following two methods to work around this behavior.

Method 1: Make Microsoft Word Visible Before the FileClose Statement

This method uses the AppShow statement to make Microsoft Word visible before using the FileClose/DocClose statement. The AppShow statement is a Microsoft WordBasic statement that can be issued through OLE automation to make Microsoft Word visible.

  1. Open the sample database Northwind.mdb.

  2. Create a module and type the following procedure:

          Sub WordTest()
          Dim objWordBasic As Object
          Set objWordBasic = CreateObject("Word.Basic")
          objWordBasic.AppShow
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 1"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 2"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          End Sub
    
    

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

           WordTest
    

    Note that Microsoft Word correctly saves the document to your C:\ folder.

Method 2: Create the Word Object Based on the Word.Document Class

This method uses two OLE automation objects. The first object is created using the Word.Document class. The second object is created off the Application.WordBasic class that is derived from the first object.

  1. Open the sample database Northwind.mdb.

  2. Create a module and type the following procedure:

          Sub WordTest()
          Dim objWordDoc As Object
          Dim objWordBasic As Object
          Set objWordDoc = CreateObject("Word.Document")
          Set objWordBasic = objWordDoc.Application.WordBasic
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 1"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 2"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          End Sub
    
    

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

           WordTest
    

    Note that Microsoft Word correctly saves the document to your C:\ folder.

MORE INFORMATION

Microsoft WordBasic uses the FileClose statement to close the currently active document and all associated document windows, and uses the DocClose statement to close only the currently active document window in Microsoft Word. When either of these statements is issued from an OLE Automation controlling application, such as Microsoft Access or Microsoft Excel, Microsoft Word is closed unexpectedly.

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.

  2. Create a module and type the following procedure:

          Sub WordTest()
          Dim objWordBasic As Object
          Set objWordBasic = CreateObject("Word.Basic")
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 1"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          objWordBasic.FileNewDefault
          objWordBasic.INSERT "This is test 2"
          objWordBasic.FileSaveAs "C:\Testword.doc", 0
          objWordBasic.FileClose 1
          End Sub
    
       NOTE: The code in this procedure uses the FileClose statement to
       demonstrate the behavior. To reproduce the behavior using the DocClose
       statement, simply substitute all occurrences of FileClose with
       DocClose.
    
    

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

           WordTest
    

    Note that when the code attempts to create the second new document, you receive the error:

          Run-time error -2147418094 (80010012): OLE Automation Error
    

    This is because Microsoft Word has been unexpectedly closed by either the FileClose or DocClose statement.

REFERENCES

For more information about the FileClose and DocClose statements, search for "FileClose statement" and "DocClose statement" using the Microsoft Word for Windows 95 Help Index.

For more information about creating objects with OLE Automation, search on the phrase "Create an object with OLE Automation," and then "CreateObject Function" or "Using Microsoft Access as an OLE Automation Controller" using the Microsoft Access for Windows 95 Answer Wizard.

Microsoft Access, "Building Applications with Microsoft Access for Windows 95," version 7.0, Chapter 11, "Communicating with Other Applications," pages 262-290.

Keywords          : AutoGnrl kbinterop kbole IntpOleA
Technology        : kbole
Version           : 7.0
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbprb
Solution Type     : kbcode


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


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: August 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.