VBA 中的错误 462:找不到远程服务器机器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5496294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 462 in VBA : remote server machine not found
提问by Thomas Mathew
following code is to read a word file in vba. But its showing an error
下面的代码是在vba中读取一个word文件。但它显示一个错误
Error 462 in VBA : remote server machine not found.
VBA 中的错误 462:未找到远程服务器计算机。
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
提前致谢
回答by JMax
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.
Visual Basic 已经建立了对 Excel 的引用,因为有一行代码调用了 Excel 对象、方法或属性,而没有使用 Excel 对象变量限定元素。在您结束程序之前,Visual Basic 不会释放此引用。当代码运行多次时,此错误引用会干扰自动化代码。
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.
若要解决此问题,请修改代码,以便使用适当的对象变量限定对 Excel 对象、方法或属性的每次调用。
Source
来源
Have a look here : http://support.microsoft.com/default.aspx?kbid=178510
看看这里:http: //support.microsoft.com/default.aspx?kbid=178510
You could also have a look here : http://www.tek-tips.com/viewthread.cfm?qid=756598
你也可以看看这里: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.
该帖子的作者收到错误,因为他没有使用 Access 对象来打开和关闭数据库。
And finally :
最后:
- you should avoid using the with block with a Word Application object
- you should free your variables (Set ... As Nothing, wrdApp.Close, ...)
- 您应该避免将 with 块与 Word Application 对象一起使用
- 你应该释放你的变量(Set ... As Nothing, wrdApp.Close, ...)
回答by Slai
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