This article was previously published under Q188546
You run a Visual Basic application that uses the CreateObject function tostart a hidden instance of Microsoft Word. The application is idle, but itstill maintains a reference to Word. Next, you manually start an instanceof Word. A separate instance of Word should open, but the same instancethat was created by the Visual Basic application is made active instead. Ifyou close Word and continue to work in the Visual Basic application, one of the following errors occurs when the application tries to use Word objectsbecause Word is no longer running:
Run-time error '462': The remote server machine does not exist or is unavailable
This automation error translates to "The RPC server is unavailable."
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
Microsoft has confirmed this to be a bug in the Microsoft products listedat the beginning of this article.
Steps to Reproduce Behavior
NOTE:Make sure Microsoft Word is not running before following these steps. Youmight need to temporarily close any Mail programs that use Microsoft Word.
Start a new Visual Basic application.
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."
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
Run the CreateWordObject procedure. A hidden instance of Microsoft Word is created.
Run the UseWordObject procedure. The message boxes should display "Application" and "Microsoft Word."
Start Microsoft Word (using the Windows Start button, Windows Explorer or the Microsoft Office toolbar, and so forth.)
Quit Microsoft Word.
Run the UseWordObject procedure.
RESULT: The first message box displays "Object", which indicates thereference to Word is no longer valid. The next message box results in therun-time error when you attempt to use the Word object.
Replace the CreateWordObject procedure with the following new procedure andrepeat the previous steps. You should no longer receive the Automationerror. Be sure to run the CloseWordObject procedure to close the hiddeninstance 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.