簡體   English   中英

使用VBA運行時錯誤462在Excel中合並Docx文件遠程服務器計算機,然后文件損壞

[英]Merging Docx Files in Excel using VBA Runtime Error 462 The remote server machine, then File Corrupted

救命! 關於此主題有很多帖子,我嘗試了其中的大多數,但仍然無濟於事。

基本上,我只是想在指定的文件夾中合並具有相同名稱的docx文件的內容。 每個文件都包含一個圖像,即1file:1image。 第一次運行代碼時出現錯誤(錯誤462遠程服務器計算機不存在或不可用),它指向

ActiveDocument.Range.InsertFile Filename:=myDir & nextFile

有人知道該如何解決嗎? 請。 這是我的代碼:

Sub Merger()
    Dim firstFileStr As String
    Dim firstFile As String
    Dim nextFileStr As String
    Dim nextFile As String

    Dim xFlag As Boolean
    Dim myDir As String

    myDir = "C:\Users\User\Desktop\MergeFolder\"
    For iCtr = 1 To getRowCount
        firstFileStr = Sheet1.Cells(iCtr, 3).Value
        firstFile = Sheet1.Cells(iCtr, 1).Value

        xFlag = True
        Dim jCtr As Integer
        jCtr = 1

        Do While (xFlag)
            nextFileStr = Sheet1.Cells(iCtr + jCtr, 3).Value
            nextFile = Sheet1.Cells(iCtr + jCtr, 1).Value

            If StrComp(firstFileStr, nextFileStr, vbTextCompare) = 0 Then
                Dim WordApp As Word.Application 'word application
                Dim WordDoc As Word.Document    'word document

                If Not WordApp Is Nothing Then
                    WordApp.Quit
                End If

                Set WordApp = New Word.Application
                Set WordDoc = WordApp.Documents.Open(myDir & firstFile, ConfirmConversions = False, ReadOnly = False)

                Application.ScreenUpdating = False

                ActiveDocument.Range.InsertFile Filename:=myDir & nextFile

                WordDoc.SaveAs Filename:=myDir & "merged " & firstFileStr, FileFormat:=wdFormatDocument
                WordDoc.Close
                WordApp.Quit

                Set WordDoc = Nothing
                Set WordApp = Nothing

                xFlag = True
                jCtr = jCtr + 1
            Else
                xFlag = False
            End If
        Loop
    Next
End Sub

函數getRowCount只是返回Sheet1包含的數字或行。 這里,

Function getRowCount() As Integer
    Dim rowCount As Integer
    rowCount = 0

    Range("A1").Select
    ' Set Do loop to stop when an empty cell is reached.
    Do While Not IsEmpty(ActiveCell)
        rowCount = rowCount + 1
        ' Step down 1 row from present location.
        ActiveCell.Offset(1, 0).Select
    Loop
    getRowCount = rowCount

End Function

現在,當我在上一個錯誤之后立即運行代碼並且沒有在任務管理器中終止MS Word進程時,我又遇到了另一個錯誤運行時錯誤5792文件似乎已損壞。 檢查新創建的文件,似乎代碼

ActiveDocument.Range.InsertFile Filename:=myDir & nextFile

根本不起作用。 excel文件看起來像(列B為空)

Column    A         B     C 
alpha - 1.docx          alpha  
alpha -2.docx           alpha  
alpha - 3.docx          alpha  
alpha - 4.docx          alpha  
bravo - 1.docx          bravo  
bravo - 5.docx          bravo  
charlie- 2.docx         charlie  
delta - 3.docx          delta  
delta - 5.docx          delta  
epsilon - 9.docx        epsilon  
foxtrot - 1.docx        foxtrot         
merger.xlsm             0 
~$merger.xlsm             0

應該將所有“ Alpha-* .docx ”合並為“ Alpha-1.docx ”,bravo-5.docx合並為bravo-1.docx等。

我已經進行了一些研究,問題是您將單詞文件打開為只讀。 確保doc一詞已關閉,並且您有權使用寫入權限打開它。

您可以通過在打開文件之后但嘗試在其中粘貼內容之前的某個時間添加一個消息框來測試它是否以只讀方式打開:

MsgBox Worddoc.readonly

暫無
暫無

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

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