Thomas Mathew
Thomas Mathew

Reputation: 1151

Error 462 in VBA : remote server machine not found

following code is to read a word file in vba. But its showing an error

Error 462 in VBA : remote server machine not found.

Sub abc()
    Dim fileReader As String
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim singleLine As Paragraph
    Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents.Open("C:\Documents and Settings\Administrator\My Documents\Downloads\fwfiles\webs.doc")
    With wrdDoc
        Dim p As Paragraph
        For Each p In wrdDoc.Paragraphs
            fileReader = p.Range.Text
        Next p
    End With
End Sub

Thanks in advance

Upvotes: 4

Views: 31482

Answers (2)

JMax
JMax

Reputation: 26611

Does it break when you launch it twice ?

Cause

Visual Basic has established a reference to Excel because of a line of code that calls an Excel object, method, or property without qualifying the element with an Excel 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 one time.

Resolution

To resolve this problem, modify the code so each call to an Excel object, method, or property is qualified with the appropriate object variable.

Source

Have a look here : http://support.microsoft.com/default.aspx?kbid=178510


You could also have a look here : http://www.tek-tips.com/viewthread.cfm?qid=756598

The author of the post was getting the error because he was not using the Access object to open and close the database.


And finally :

  • you should avoid using the with block with a Word Application object
  • you should free your variables (Set ... As Nothing, wrdApp.Close, ...)

Upvotes: 5

Slai
Slai

Reputation: 22896

or you can try this shorter version:

Function abc() As String
    doc = "C:\Documents and Settings\Administrator\My Documents\Downloads\fwfiles\webs.doc"
    Set objDoc = GetObject(doc, "Word.Application")
    abc = doc.Range.Text
    objDoc.Close
    objDoc = Nothing
End

Upvotes: 2

Related Questions