Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

You may receive the "Run-time error '-2147023174' (800706ba)" error message or the "Run-time error '462'" when you run Visual Basic code that uses Automation to control Word


View products that this article applies to.

Symptoms

When you run Microsoft Visual Basic code that uses Automation to control Microsoft Word, you may receive one of the following error messages:

Error message 1
Run-time error '-2147023174' (800706ba)
Automation error
Error message 2
Run-time error '462': The remote server machine does not exist or is unavailable

↑ Back to the top


Cause

Visual Basic has established a reference to Word due to a line of code that calls a Word object, method, or property without qualifying it with a Word object variable. Visual Basic does not release this reference until you end the program. This errant reference interferes with automation code when the code is run more than once.

↑ Back to the top


Resolution

Modify the code so that each call to a Word object, method, or property is qualified with the appropriate object variable.

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More Information

To automate Word, you establish an object variable that usually refers to the Word Application or Document object. Other object variables can then be set to refer to a Selection, a Range, or other objects in the Word object model. When you write code to use a Word object, method, or property, you should always precede the call with the appropriate object variable. If you do not, 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.


The following steps illustrate how to reproduce this problem, and how to correct it.

Steps to Reproduce Behavior

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Click References from the Project menu, and then click one of the following options:
    • For Office Word 2007, click Microsoft Word 12.0 Object Library
    • For Word 2003, click Microsoft Word 11.0 Object Library
    • For Word 2003, click Microsoft Word 10.0 Object Library
    • For Word 2000, click Microsoft Word 9.0 Object Library.
    • For Word 97, click Microsoft Word 8.0 Object Library.
  3. Place a CommandButton on Form1.
  4. Copy the following code to the Code Window of Form1:
          Option Explicit

    Private Sub Command1_Click()
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oRange as Word.Range

    Set oWord = CreateObject("Word.Application")
    With oWord
    .Visible = True
    .Activate
    .WindowState = wdWindowStateNormal
    End With

    Set oDoc = oWord.Documents.Add
    MsgBox "Document open", vbMsgBoxSetForeground
    With oDoc
    .PageSetup.LeftMargin = InchesToPoints(1.25)
    End With

    ' This example inserts text at the end of section one.
    Set oRange = ActiveDocument.Sections(1).Range
    With oRange
    .MoveEnd Unit:=wdCharacter, Count:= -1
    .Collapse Direction:=wdCollapseEnd
    .InsertParagraphAfter
    .InsertAfter "End of section."
    End With

    With oDoc
    .Saved = True
    End With

    Set oRange = Nothing
    Set oDoc = Nothing
    oWord.Quit
    Set oWord = Nothing
    End Sub
  5. On the Run menu, click Start or press the F5 key to start the program.
  6. Click the CommandButton. No error occurs. However, a reference to Word has been created and has not been released.
  7. Click the CommandButton again and note that you receive the error previously described.

    Note The error occurs because the code refers to the InchesToPoints Method without preceding the call with the oWord object variable.
  8. Stop the project and change the following line:
    .PageSetup.LeftMargin = InchesToPoints(1.25)
    -to-


    .PageSetup.LeftMargin = oWord.InchesToPoints(1.25)
  9. Run the program again. Then, click the CommandButton. No error occurs.
  10. Click the CommandButton again and note that you receive the error.

    Note The error occurs because the code refers to the ActiveDocument Section one's Range object without preceding the call with the oWord object variable.
  11. Stop the project and change the following line:
    Set oRange = ActiveDocument.Sections(1).Range
    -to-

    Set oRange = oWord.ActiveDocument.Sections(1).Range
  12. Run the program again. Note that you can run the code multiple times without error.
When building a Visual Basic project automating Word, if your project has a reference to the Microsoft Word Object Library, sample code for the objects, methods, and properties of the Word Object Model is available from the Word Help file. When the cursor is over a key word in your code, you will see any applicable Help text by pressing the F1 key.


The sample code in the Help topic will be the Microsoft Word Visual Basic for Applications code. It will not show the object references that your Visual Basic code requires. You will need to add the qualifiers as appropriate.

↑ Back to the top


References

For additional information, please see the following articles in the Microsoft Knowledge Base:

178510 PRB: Excel Automation Method of Object '_Global'Failed
167223 Microsoft Office 97 Automation Help File Available
For additional information about the Automation of Office applications, click the article number below to view the article in the Microsoft Knowledge Base:

222101 HOWTO: Find and Use Office Object Model Documentation

↑ Back to the top


Keywords: kboffice12yes, kbfreshness2006, kbexpertiseinter, kbprogramming, kbautomation, kbsweptsoltax, kbprb, kbbillprodsweep, kb, kberrmsg

↑ Back to the top

Article Info
Article ID : 189618
Revision : 6
Created on : 8/20/2020
Published on : 8/20/2020
Exists online : False
Views : 429