簡體   English   中英

錯誤 462:通過 Excel VBA 使用 Word 時遠程服務器計算機不存在

[英]Error 462: The remote server machine does not exist when working with Word via Excel VBA

我在 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。 有什么原因嗎?

您有兩個對 Word 對象的非限定引用:

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

出現兩次,需要是:

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

否則,您將創建無法在代碼中銷毀的對 Word 的隱式引用。

您應該首先確保任務管理器中沒有 oprhan winword.exe 然后殺死或注銷/登錄以擺脫它們。

然后,您應該在“明確”關閉詞的末尾添加類似以下代碼的內容:

(我不確定確切的語法,希望你能解決)

IF Not(objWord Is Nothing) Then

    objWord.Close(False)
    Set objWord = Nothing

End If

您應該添加類似於錯誤處理程序的內容。

經常發生的是在開發和調試過程中,有時單詞沒有被正確關閉並且“孤兒”進程掛起,即使它們不可見。

您可能還希望使用

Set objWord = New Word.Application

代替

Set objWord = CreateObject("Word.Application")

因為這會給你自動完成等。

但每種方式都有區域優勢。

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

嘗試以編程方式設置選項卡時,我遇到了同樣的問題。 錯誤消息“錯誤 462:遠程服務器不存在”時不時出現,但在新啟動的 Access 后首次運行時不會出現。

似乎是全局“CentimetersToPoints(5)”導致了這個問題。 我替換了 oRng.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(5), Alignment:=wdAlignTabLeft,Leader:=wdTabLeaderSpaces 通過 Call oRng.Paragraphs.TabStops.Add(142, wdAlignTabLeft, wdTabLeader) 執行相同但沒有錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM