2017-03-07 43 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代表。 –

相关问题