Results 1 to 5 of 5

Thread: Excel 2007 and Acrobat Reader VBA code to combine multiple PDF files into one PDF

  1. #1

    Excel 2007 and Acrobat Reader VBA code to combine multiple PDF files into one PDF

    Hi Guys

    I have the source folder :

    The destination folder is the same

    And i need a VBA code in Excel 2007 that can combine all the pdf files from that folder into a single one.

    Do you have any idea ?

  2. #2
    It will help others help you if you post your complete code.

    Also, have you seen this post?
    excel - VBA, Combine PDFs into one PDF file - Stack Overflow

  3. #3
    Hi Jdelano and thanks for taking the time to respond
    I will check out the link you provided
    This is the code:
    Any help will be appreciated
    Thanks a lot
    God Bless!

  4. #4
    Sub Main()   
        Const DestFile As String = "MergedFile.pdf" ' <-- change to suit
        Dim MyPath As String, MyFiles As String
        Dim a() As String, i As Long, f As String
         ' Choose the folder or just replace that part by: MyPath = Range("E3")
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\MIHAI\DOC\ASIG\DOSARE\"
            .AllowMultiSelect = False
            If .Show = False Then Exit Sub
            MyPath = "C:\MIHAI\DOC\ASIG\DOSARE\"
        End With
          ' 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()
        ' Merge PDFs
        If i Then
            ReDim Preserve a(1 To i)
            MyFiles = Join(a, ",")
            Application.StatusBar = "Merging, please wait ..."
            Call MergePDFs(MyPath, MyFiles, DestFile)
            Application.StatusBar = False
            MsgBox "No PDF files found in" & vbLf & MyPath, vbExclamation, "Canceled"
        End If
    End Sub
    Sub MergePDFs(MyPath As String, MyFiles As String, Optional DestFile As String = "MergedFile.pdf")
    ' ZVI:2013-08-27
    ' Reference required: VBE - Tools - References - Acrobat
        Dim a As Variant, i As Long, n As Long, ni As Long, p As String
        Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
        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
                Set PartDocs(i) = Nothing
                ' Calc the number of pages in PartDocs(0) document
                n = PartDocs(0).GetNumPages()
            End If
        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
        ' 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
        Set AcroApp = Nothing
    End Sub
    End Sub

  5. #5
    AcroApp.Quit is what I would expect this line to be. Although, you never use that object directly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts