Error 462 in VBA : remote server machine not found

佐手、 提交于 2019-12-07 12:39:42

问题


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


回答1:


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, ...)



回答2:


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


来源:https://stackoverflow.com/questions/5496294/error-462-in-vba-remote-server-machine-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!