BUG: Starting Word Manually Uses Same Instance as Automation (188546)



The information in this article applies to:

  • Microsoft Visual Basic for Applications 5.0
  • Microsoft Office XP Developer
  • Microsoft Office 2000 Developer
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows

This article was previously published under Q188546

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, one of the following errors occurs when the application tries to use Word objects because Word is no longer running:
Run-time error '462':
The remote server machine does not exist or is unavailable

-or-

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. This workaround is only for Microsoft Word 97. 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." For Word 2002, set the reference to the "Microsoft Word 10.0 Object Library."
  3. Create a new Module and copy the following code into the module:
          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  SaveChanges:=wdDoNotSaveChanges
                  Set wrdApp = Nothing
              End If
          End Sub
    					
  4. Run the CreateWordObject procedure. A hidden instance of Microsoft Word is created.
  5. Run the UseWordObject procedure. The message boxes should display "Application" and "Microsoft Word."
  6. Start Microsoft Word (using the Windows Start button, Windows Explorer or the Microsoft Office toolbar, and so forth.)
  7. Quit Microsoft Word.
  8. 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 run-time 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 Windows Explorer, typically when someone double-clicks a document.

Modification Type:MinorLast Reviewed:3/9/2005
Keywords:kbAutomation kbbug kbpending KB188546