BUG: Starting Word Manually Uses Same Instance as Automation

ID: Q188546


The information in this article applies to:
  • Microsoft Visual Basic for Applications version 5.0
  • Microsoft Office 2000 Developer
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows


SYMPTOMS

You run a Visual Basic application that uses the CreateObject function to start a hidden instance of Microsoft Word. The application is idle, but it still maintains a reference to Word. Next, you manually start an instance of Word. A separate instance of Word should open, but the same instance that was created by the Visual Basic application is made active instead. If you close Word and continue to work in the Visual Basic application, the following error occurs when the application tries to use Word objects because Word is no longer running:

Run-time error '-2147023174 (800706ba)':
Automation error
This automation error translates to "The RPC server is unavailable."


RESOLUTION

Use one of the following to work around this problem:

  • Before you create your Word object, first create a temporary Word object. After you create your object, close the temporary object. This causes Word to act properly when you control it through Automation (that is, if a user interactively starts Word, a new instance of Word is opened for the user). The automation instance remains hidden and separate. See the Steps to Reproduce Behavior section for an example of this workaround.


  • Make the Word object visible immediately after using the CreateObject function. For example:


  • 
          Set wrdApp = CreateObject("Word.Application")
          wrdApp.Visible = True 


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

Steps to Reproduce Behavior

NOTE:
  • Make sure Microsoft Word is not running before following these steps. You might need to temporarily close any Mail programs that use Microsoft Word.


    1. Start a new Visual Basic application.


    2. Set a reference to the "Microsoft Word 8.0 Object Library." For Word 2000, set the reference to the "Microsoft Word 9.0 Object Library."


    3. Create a new Module and copy the following code into the module:


    4. 
            Option Explicit
      
            Dim wrdApp As Word.Application
      
            Private Sub CreateWordObject()
                'Test if object is already created before calling CreateObject:
                If TypeName(wrdApp) <> "Application" Then
                    Set wrdApp = CreateObject("Word.Application")
                End If
            End Sub
      
            Private Sub UseWordObject()
                MsgBox TypeName(wrdApp) 'if displays "Application" then
                    'Reference to Word is valid, else reference is invalid and
                    'an error occurs on the following line:
                MsgBox wrdApp.Name
            End Sub
      
            Private Sub CloseWordObject()
                If TypeName(wrdApp) = "Application" Then
                    wrdApp.Quit
                    Set wrdApp = Nothing
                End If
            End Sub 
    5. Run the CreateWordObject procedure. A hidden instance of Microsoft Word is created.


    6. Run the UseWordObject procedure. The message boxes should display "Application" and "Microsoft Word."


    7. Start Microsoft Word (using the Windows Start button, Windows Explorer or the Microsoft Office toolbar, and so forth.)


    8. Quit Microsoft Word.


    9. Run the UseWordObject procedure.

      RESULT: The first message box displays "Object", which indicates the reference to Word is no longer valid. The next message box results in the Automation error when you attempt to use the Word object.


    Workaround

    Replace the CreateWordObject procedure with the following new procedure and repeat the previous steps. You should no longer receive the Automation error. Be sure to run the CloseWordObject procedure to close the hidden instance of Word:
    
        Private Sub CreateWordObject()
            Dim temp As Word.Application
            'Test if object is already created before calling CreateObject:
            If TypeName(wrdApp) <> "Application" Then
                Set temp = CreateObject("Word.Application")
                Set wrdApp = CreateObject("Word.Application")
                temp.Quit
                Set temp = Nothing
            End If
        End Sub 
    NOTE: This workaround does not work when a document is launched directly through the Window Explorer, typically when someone double-clicks a document.

    Additional query words: OLE automation

    Keywords : kbAutomation kbWord kbGrpDSO kbOffice2000 kbVBA500 kbword2000
    Version : WINDOWS:2000,5.0,97; :
    Platform : WINDOWS
    Issue type : kbbug


    Last Reviewed: June 3, 1999
    © 2000 Microsoft Corporation. All rights reserved. Terms of Use.