【问题标题】:Error 462: The remote server machine does not exist when working with Word via Excel VBA错误 462:通过 Excel VBA 使用 Word 时,远程服务器计算机不存在
【发布时间】:2022-02-08 03:50:19
【问题描述】:

我在 Excel VBA 中以编程方式打开一个 Word 文件并使用书签添加/编辑内容。

我发现在交替运行时,我得到了

错误 462:远程服务器不存在

我研究并理解这与“不合格的引用”有关。

我不明白如何将代码更正为合格的引用。

        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

此外,无论我定义什么 exR 范围,它都会完成整个范围的执行,但最后会调用 MyErrorHandler。有什么原因吗?

【问题讨论】:

  • 您的错误处理程序被调用,因为您没有在到达子程序之前退出它。在MyErrorHandler: 之前添加exit sub。我也没有在任何地方看到On Error Goto
  • @Nick.McDermaid 谢谢,这解决了一个问题......但主要问题仍然是遇到的错误 462。你能帮我解决这个问题吗? :(

标签: excel vba


【解决方案1】:

您有两个对 Word 对象的非限定引用:

objSelection.Style = ActiveDocument.Styles("Heading 1")

出现两次,需要是:

objSelection.Style = objWord.ActiveDocument.Styles("Heading 1")

否则,您将创建一个无法在代码中销毁的对 Word 的隐式引用。

【讨论】:

  • 谢谢伙计,这似乎暂时有效。让我回复你,以防我再次遇到这个问题。现在至少有几次,我没有遇到这个问题。再次非常感谢,上帝保佑!
【解决方案2】:

您应该首先确保任务管理器中没有 oprhan winword.exe。然后杀死或注销/登录以摆脱它们。

那么你应该在末尾添加类似这样的代码来“明确地”关闭词:

(我不确定具体的语法,希望你能解决)

IF Not(objWord Is Nothing) Then

    objWord.Close(False)
    Set objWord = Nothing

End If

您应该添加与错误处理程序类似的内容。

经常发生的情况是在开发和调试过程中,有时 word 没有正确关闭,“孤立”进程即使不可见也会挂起。

您可能还希望使用

Set objWord = New Word.Application

而不是

Set objWord = CreateObject("Word.Application")

因为这会给你自动完成等功能。

但每种方式都有区域优势。

【讨论】:

    【解决方案3】:

    [已解决] 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
    

    【讨论】:

      【解决方案4】:

      我在尝试以编程方式设置选项卡时遇到了同样的问题。错误消息“错误 462:远程服务器不存在”时不时出现,但在新启动 Access 后第一次运行时从未出现。

      似乎是全局“CentimetersToPoints(5)”引起了问题。 我换了

      oRng.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(5), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
      

      通过

      Call oRng.Paragraphs.TabStops.Add(142, wdAlignTabLeft, wdTabLeaderSpaces) 
      

      执行相同但没有错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 2021-10-03
        • 1970-01-01
        • 2015-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多