0% found this document useful (0 votes)
7 views

vba-error-462-explained-and-resolved

The document explains VBA Error 462, which occurs when attempting to run code that opens Word from Access multiple times without properly referencing the Word application. It details the cause of the error, which is due to the ActiveDocument object not being fully qualified, and provides a solution by ensuring all calls are qualified with the Word application instance. Additionally, it notes that Word must be installed and a reference to the Word library must be set in the VBA Editor for successful automation.

Uploaded by

Bruno
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

vba-error-462-explained-and-resolved

The document explains VBA Error 462, which occurs when attempting to run code that opens Word from Access multiple times without properly referencing the Word application. It details the cause of the error, which is due to the ActiveDocument object not being fully qualified, and provides a solution by ensuring all calls are qualified with the Word application instance. Additionally, it notes that Word must be installed and a reference to the Word library must be set in the VBA Editor for successful automation.

Uploaded by

Bruno
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

VBA Error 462 Explained and Resolved


1 C French, Nov 11
The code to start Word and open a specific document will typically be similar to this:

Dim objApplication As Object

Dim strDoc As String

Set objApplication = CreateObject("Word.Application")

objApplication.Visible = True

strDoc = Application.CurrentProject.Path & "\memo from head of year 9.docx"

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.

'Inserts text from fields in query into bookmark positions

objApplication.ActiveDocument.Bookmarks("bkVenue").Select

objApplication.ActiveDocument.Application.Selection.InsertAfter
strVenue

'Saves the edited memo with information in default folder

ActiveDocument.SaveAs strMemo

'Optionally, print the documents

objApplication.ActiveDocument.PrintOut

The final section of code closes the document and quits Word.

objApplication.ActiveDocument.Save

objApplication.ActiveDocument.Close

objApplication.Quit

Set objApplication = Nothing

The code in the second section above contains an error; running the code twice will produce Error
462.

VBA Error 462 Explained and Resolved


2 C French, Nov 11
How to Resolve the Problem
The error occurs because the ActiveDocument object has not been fully qualified by reference to the
Office object – the Word application – in every case. The line of code that saves the document must
be modified to include a specific reference to the instance of Word, as highlighted below.

'Inserts text from fields in query into bookmark positions

objApplication.ActiveDocument.Bookmarks("bkVenue").Select

objApplication.ActiveDocument.Application.Selection.InsertAfter
strVenue

'Saves the edited memo with information in default folder

objApplication.ActiveDocument.SaveAs strMemo

'Optionally, print the documents

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.

Knowledge Base article http://support.microsoft.com/kb/189618 explains

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].

Notes on the Sample Code


This document is intended to explain error code 462, not to provide a complete explanation of how
to create a Word document from Access.

Two small points:

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.

VBA Error 462 Explained and Resolved


3 C French, Nov 11

You might also like