第二次运行代码时出现远程服务器机器462错误

时间:2018-03-08 21:04:06

标签: excel vba ms-word

我正在尝试创建一个比较2个文档的宏,并打开比较文档。在第一次运行时它工作正常,但在第二次,它会出现错误462 - 远程服务器机器不存在或不可用。

我已经阅读了与此相关的其他帖子,但没有一个解决方案似乎可以防止我的宏上的错误。

任何想法都将不胜感激!!

由于

Sub Compare()


        Dim wordApp As Word.Application
        Dim objMain, objComp As Word.Document

        Dim wb As Excel.Workbook
        Set wb = Workbooks("test.xlsm")

        Dim ws As Excel.Worksheet
        Set ws = wb.Sheets("Sheet1")

        On Error Resume Next
        Set wordApp = GetObject(, "Word.Application")
        If wordApp Is Nothing Then
            Set wordApp = CreateObject("Word.Application")
        End If

        path = Excel.Application.ActiveWorkbook.path

        ''''''''''PUT FILE NAMES NEXT TO EACH OTHER GOING DOWN ON EXCEL, SO FRL on A1 and A2, STAGE 2 FRL on B1 and B2 etc

        For i = 1 To 2

            ''''THIS IS FOR THE CASE HANDLERS SAVED TEMPLATES
            On Error Resume Next
            Set objMain = wordApp.Documents.Open(path & "\..\" & ws.Range("O" & i)) '''''THIS WILL PICK THE RANGE ON EXCEL WHICH HAS THE PATH NAME FOR THE FILE

            On Error GoTo lostFile
            Select Case i
                ''''''''''''THESE WILL NEED TO BE THE ACTUAL PATHS TO THE TEMPLATES
                Case 1: Set objMain = wordApp.Documents.Open("C:\Users\daryl\Desktop\excelTest\test1.docx") '''''STAGE 1 FRL
                Case 2: Set objMain = wordApp.Documents.Open("C:\Users\daryl\Desktop\excelTest\test11.docx") '''''STAGE 2 FRL
                'etc etc
            End Select


            ''''THIS IS FOR THE TEMPLATES ON SHAREPOINT
            On Error Resume Next
            Set objComp = wordApp.Documents.Open(path & "\..\" & ws.Range("P" & i)) '''''THIS WILL PICK THE RANGE ON EXCEL WHICH HAS THE PATH NAME FOR THE FILE

            On Error GoTo lostFile2
            Select Case i
                ''''''''''''THESE WILL NEED TO BE THE ACTUAL PATHS TO THE TEMPLATES
                Case 1: Set objComp = wordApp.Documents.Open("C:\Users\daryl\Desktop\excelTest\test2.docx") '''''STAGE 1 FRL
                Case 2: Set objComp = wordApp.Documents.Open("C:\Users\daryl\Desktop\excelTest\test22.docx") '''''STAGE 2 FRL
                'etc etc
            End Select

            ''''''''''''''''IF BOTH FILES FOUND, CONTINUE ON!''''''''''''''''''''''''''''

            On Error GoTo 0
            wordApp.Visible = True
            objMain.Activate

            With wordApp
                .ActiveDocument.TrackRevisions = True
                .TrackRevisions = True
                .Application.CompareDocuments OriginalDocument:=Documents(objMain), RevisedDocument:=Documents(objComp), Destination:=wdCompareDestinationNew, _
                    Granularity:=wdGranularityWordLevel, CompareFormatting:=True, _
                    CompareCaseChanges:=True, CompareWhitespace:=True, CompareTables:=True, _
                    CompareHeaders:=True, CompareFootnotes:=True, CompareTextboxes:=True, _
                    CompareFields:=True, CompareComments:=True, CompareMoves:=True, _
                    RevisedAuthor:="Daryl Boakes", IgnoreAllComparisonWarnings:=False
                .ActiveWindow.ShowSourceDocuments = wdShowSourceDocumentsBoth
                .ActiveWindow.View.SplitSpecial = wdPaneRevisions

            End With

            objMain.Close
            objComp.Close

        Next i

        wordApp.Activate

        Exit Sub

    lostFile:
        MsgBox "The first file cannot be found", vbOKOnly
        GoTo finish

    lostFile2:
        MsgBox "The second file cannot be found", vbOKOnly
        GoTo finish

    finish:


    End Sub

0 个答案:

没有答案