Microsoft KB Archive/156226

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

Article ID: 156226

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 95 Standard Edition

-



This article was previously published under Q156226



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.

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

 Open the sample database Northwind.mdb.  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 </li> 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.</li></ol>

<div class="moreinformation_section">

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
 Open the sample database Northwind.mdb.</li>  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. </li> 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.</li></ol>

<div class="references_section">