错误 462:通过 Excel VBA 使用 Word 时远程服务器计算机不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/26030584/
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: The remote server machine does not exist when working with Word via Excel VBA
提问by Sunny D'Souza
I open a Word file programmatically in Excel VBA and add/edit contents using bookmarks.
我在 Excel VBA 中以编程方式打开一个 Word 文件并使用书签添加/编辑内容。
I find that on alternate runs, I get
我发现在交替运行时,我得到
Error 462: The remote server does not exist
错误 462:远程服务器不存在
I researched and understood this has something to do with 'Unqualified references'.
我研究并理解这与“不合格的引用”有关。
I don't understand how to correct the code to qualified references.
我不明白如何将代码更正为合格的引用。
        Set exR = ActiveSheet.Range(TestIdCol & CStr(DataRowNum) & ":" & TestIdCol & CStr(RowEnd))
           ExistingEvidenceDoc = UseFileDialogOpen("Word Documents", "*.doc;*.docx")
           Set objWord = CreateObject("Word.Application")
           If ExistingEvidenceDoc <> "" Then
                Set objDoc = objWord.Documents.Open(ExistingEvidenceDoc)
           Else
                Exit Sub
           End If
           objWord.Visible = True
           Application.Wait Now() + TimeSerial(0, 0, 5)
           Set objSelection = objWord.Selection
           getExistingEvidences = ExistingTestEvidences(objDoc)
           o = DataRowNum
            For Each cell In exR
                If cell.Value <> "" And Not IsInArray(cell.Value, getExistingEvidences) Then
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight
                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph
                ElseIf IsInArray(cell.Value, getExistingEvidences) = False Then
                    objSelection.EndKey
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight
                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph
                End If
                o = o + 1
            Next cell
        MyErrorHandler:
                MsgBox "SeeHeadingPageNumber" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description
Additionaly, whatever exR range I define, it completes execution for the entire range but at the end MyErrorHandler is invoked. Is there a reason for it?
另外,无论我定义什么 exR 范围,它都会完成整个范围的执行,但最后 MyErrorHandler 被调用。有什么原因吗?
回答by Rory
You have two unqualified references to Word objects:
您有两个对 Word 对象的非限定引用:
objSelection.Style = ActiveDocument.Styles("Heading 1")
which appears twice, needs to be:
出现两次,需要是:
objSelection.Style = objWord.ActiveDocument.Styles("Heading 1")
Otherwise you're creating an implicit reference to Word that you can't destroy in your code.
否则,您将创建无法在代码中销毁的对 Word 的隐式引用。
回答by Nick.McDermaid
You should first ensure there are no oprhan winword.exein task manager. Kill then or log out/in to get rid of them.
您应该首先确保winword.exe任务管理器中没有 oprhan 。然后杀死或注销/登录以摆脱它们。
Then you should add something like this code to the end to 'explcitly' close word:
然后,您应该在“明确”关闭词的末尾添加类似以下代码的内容:
(I'm not sure of the exact syntax, hopefully you can work it out)
(我不确定确切的语法,希望你能解决)
IF Not(objWord Is Nothing) Then
    objWord.Close(False)
    Set objWord = Nothing
End If
You should add something similar to your error handler.
您应该添加类似于错误处理程序的内容。
What often happens is during development and debugging, sometimes word doesn't get closed properly and 'orphan' processes hang around even though they are not visible.
经常发生的是在开发和调试过程中,有时单词没有被正确关闭并且“孤儿”进程挂起,即使它们不可见。
You may also wish to use
您可能还希望使用
Set objWord = New Word.Application
instead of
代替
Set objWord = CreateObject("Word.Application")
as that gives you autocomplete etc.
因为这会给你自动完成等。
But there area advantages to each way.
但每种方式都有区域优势。
回答by Gajendra Santosh
[SOLVED]Err 462 – "The remote server machine does not exist or is unavailable"
[已解决]Err 462 – “远程服务器机器不存在或不可用”
ErrResume:
    On Error GoTo ErrPaste
        Set objDoc = objWord.Documents.Open(ExistingEvidenceDoc)
    On Error GoTo 0
ErrPaste:
  'The remote server machine does not exist or is unavailable
    If Err.Number = 462 Then
        Set wdApp = CreateObject("Word.Application")
        Resume ErrResume
    End If

