2017-03-07 59 views
0

我想讓它打印C2中的文件夾中的文件,然後繼續重複C3。它在我第一次運行它時運行良好,但是當它嘗試C3時,它輸出錯誤序列,它不能插入頁面,然後無法保存並且沒有輸出文件。我對VBA的基本理解讓我覺得其中一個整數或數組不是'重置'。爲什麼這個VBA代碼不會循環?

您認爲如何?我該如何修復,以便通過幾個不同的文件夾循環輸出。

Sub MergePDFs() 

    Dim a() As String, i As Long, n As Long, ni As Long, p As String, f As String 
    Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc 
    Dim DestFile As String '<-- change to suit 
    Dim t As Integer 
    Dim MyPath As String, MyFiles As String 

    ' Choose the folder or just replace that part by: MyPath = Range("E3") 
     '.InitialFileName = "C:\Temp\" 
    For t = 0 To 1 
    MyPath = Cells(t + 2, 3).Value 
    DestFile = Cells(t + 2, 1).Value & ".pdf" 

    ' Populate the array a() by PDF file names 
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\" 
    ReDim a(1 To 2^14) 
    f = Dir(MyPath & "*.pdf") 
    While Len(f) 
     If StrComp(f, DestFile, vbTextCompare) Then 
      i = i + 1 
      a(i) = f 
     End If 
     f = Dir() 
    Wend 

    ' Merge PDFs 
    If i Then 
     ReDim Preserve a(1 To i) 
     MyFiles = Join(a, ",") 
     Application.StatusBar = "Merging, please wait ..." 
     Application.StatusBar = False 


    If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\" 
    a = Split(MyFiles, ",") 
    ReDim PartDocs(0 To UBound(a)) 

    On Error GoTo exit_ 
    If Len(Dir(p & DestFile)) Then Kill p & DestFile 
    For i = 0 To UBound(a) 
     ' Check PDF file presence 
     If Dir(p & Trim(a(i))) = "" Then 
      MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled" 
      Exit For 
     End If 
     ' Open PDF document 
     Set PartDocs(i) = CreateObject("AcroExch.PDDoc") 
     PartDocs(i).Open p & Trim(a(i)) 
     If i Then 
      ' Merge PDF to PartDocs(0) document 
      ni = PartDocs(i).GetNumPages() 
      If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then 
       MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled" 
      End If 
      ' Calc the number of pages in the merged document 
      n = n + ni 
      ' Release the memory 
      PartDocs(i).Close 
      Set PartDocs(i) = Nothing 
     Else 
      ' Calc the number of pages in PartDocs(0) document 
      n = PartDocs(0).GetNumPages() 
     End If 
    Next 

    If i > UBound(a) Then 
     ' Save the merged document to DestFile 
     If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then 
      MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled" 
     End If 
    End If 

exit_: 

    ' Inform about error/success 
    If Err Then 
     MsgBox Err.Description, vbCritical, "Error #" & Err.Number 
    ElseIf i > UBound(a) Then 
     MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done" 
    End If 

    ' Release the memory 
    If Not PartDocs(0) Is Nothing Then PartDocs(0).Close 
    Set PartDocs(0) = Nothing 

    ' Quit Acrobat application 
    AcroApp.Exit 
    Set AcroApp = Nothing 
    Else 
     MsgBox "No PDF files found in" & vbLf & MyPath, vbExclamation, "Canceled" 
    End If 
    Next t 
End Sub 
+0

錯誤發生在哪條線上?如果你拿出錯誤處理程序,你會得到什麼錯誤? – BruceWayne

+0

除非您使用靜態局部變量或全局變量,否則在調用之間「粘住」值應該沒有問題。 –

+0

你是什麼意思取出錯誤處理程序?對不起,我是一個新手。 –

回答

2

For t循環開始後,復位i值:

For t = 0 To 1 
    i = 0 

如果您目前擁有的第一個目錄的5個文件,從第二目錄中的文件將目前被放置在在你陣列的6+位置,前五個位置被設置爲空白。當您嘗試訪問這些「空白」文件名時,這無疑會導致問題。

通過重置您的計數器,新的文件名將被放置在陣列的1+位置。

+0

你是我的英雄。這是成功的。謝謝! –

+0

@MattCottrill如果你接受答案並投票,我會第一次在「一天之內打200 +代表」併爲自己贏得一枚新徽章! – YowE3K

+0

啊我已經upvoted你第二次我看到它的工作:/我是一個新用戶,所以它不顯示或東西,直到我有15代表。 –

相關問題