vba-error-462-explained-and-resolved
vba-error-462-explained-and-resolved
Problem - Symptoms
You create a module to open another program from an Office application. It runs successfully. You
try to run it again and get the error message
Error 462: The remote server machine does not exist or is unavailable
The explanation and example below are related to Access but the same principles apply to other
Office applications.
Problem – Explanation
One of the advantages of a suite such as Microsoft Office is that all the programs should work well
together, with each program doing what it does best. This process is called automation. In practice it
means that there is no need for the user to open Word and run a mailmerge from Word, linking it to
an Access table or Excel spreadsheet. Using VBA, the developer can achieve the same effect, opening
Word programmatically (ie using VBA code), creating, saving and (if appropriate) printing documents
and closing Word without the user having to leave Access.
In this scenario, Access is the client and Word acts as the server.
There are many books and websites that explain how to open Word from within Access or Excel and
how to create documents. A quick Internet search will find many requests for help from people who
have successfully made the link and run it once but on attempting to run their code a second time,
receive this error.
The server in the error message above is Word – or whichever program the VBA code opens.
objApplication.Visible = True
objApplication.Documents.Open strDoc
This code uses the variable type of Object to tell VBA which program is to be used.
CreateObject function starts an instance of the program – in this case, Word. Even if Word is
already running, a new instance is created by this code; this means that the code module will not
interfere with any other work that the user is doing in Word. The remaining lines make Word visible
and open the required document.
The next section of code will typically insert text from a table or query in the document and save it.
objApplication.ActiveDocument.Bookmarks("bkVenue").Select
objApplication.ActiveDocument.Application.Selection.InsertAfter
strVenue
ActiveDocument.SaveAs strMemo
objApplication.ActiveDocument.PrintOut
The final section of code closes the document and quits Word.
objApplication.ActiveDocument.Save
objApplication.ActiveDocument.Close
objApplication.Quit
The code in the second section above contains an error; running the code twice will produce Error
462.
objApplication.ActiveDocument.Bookmarks("bkVenue").Select
objApplication.ActiveDocument.Application.Selection.InsertAfter
strVenue
objApplication.ActiveDocument.SaveAs strMemo
objApplication.ActiveDocument.PrintOut
Although the final section of code appears to close Word, if you run the code with the faulty line
(without the highlighted reference) and go to the Task Manager, you will find that Word is still
running.
If you do not [qualify each call with the appropriate Object variable], Visual Basic uses a hidden global variable reference
which it sets to the currently running instance. If Word is shutdown, or if the declared object variable is released, the
hidden global variable will now reference an invalid (destroyed) object. When running the automation code again, calls
to this hidden object variable will fail with the aforementioned error {error 462].
1. Word must be installed on the PC. This will usually not be a problem.
2. To allow the client program (in this case, Access) to use objects from the Automation server
(in this case Word), you must set a reference to Word.
From within the VBA Editor, choose Tools References and tick the appropriate library. For
Word 2010 this is Microsoft Word 14.0 Object Library.