2017-03-07 12 views
0

私はC2のフォルダからファイルを印刷するようにしていますが、それはC3のために繰り返されます。初めて実行するときはうまく動作しますが、C3を試すと、ページを挿入できないエラーシーケンスが出力され、保存できず、出力ファイルもありません。私の基本的なVBAの理解では、整数や配列のいずれかが「リセット」されていないと思います。なぜこのVBAコードがループしないのですか?

あなたはどう思いますか?どのように修正すれば、出力するためにいくつかの異なるフォルダをループするでしょう。あなたのFor tループの開始後

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

iの値をリセットします。

For t = 0 To 1 
    i = 0 

あなたが現在最初のディレクトリにある5つのファイルがある場合は、2番目のディレクトリからファイルが現在に配置されます最初の5つの位置が空白に設定された状態で、配列の6+の位置に移動します。後でそれらの "空の"ファイル名にアクセスしようとすると、間違いなく問題が発生します。

カウンタをリセットすると、新しいファイル名が配列の1+の位置に配置されます。

+0

あなたは私のヒーローです。それは成功しました。ありがとうございました! –

+0

@MattCottrillあなたが答えを受け入れて投票すると、私は初めて "200 + rep in one day"になり、初めて新しいバッジを獲得します! – YowE3K

+0

ああ私はそれが働いていたのを見て、あなたをもう一度upvoted:/私は15人の担当者を持っているまで、それは表示されないか、何か新しいユーザーです。 –

関連する問題