Consulting

Results 1 to 4 of 4

Thread: Merging multiple PDFs into single file

  1. #1
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    2
    Location

    Merging multiple PDFs into single file

    I would post in the following thread but it is closed. I have listed the title of it since I can't post the link:

    Need code to merge PDF files in a folder using adobe acrobat x

    I need to change the code so that it only combines the first page of each of the PDFs to be merged. The reason for doing so is that I have a lot of PDFS to go through and want to keep the file size down. I have tried changing the variable n=1 but that hasn't worked. Thanks for any help you can provide.

    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:\Temp\"
            .AllowMultiSelect = False
            If .Show = False Then Exit Sub
            MyPath = .SelectedItems(1)
            DoEvents
        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()
        Wend
       
        ' 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
        Else
            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")
     
        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
                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
     
    End Sub

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum!

    'ni = PartDocs(i).GetNumPages()
    ni = 1

  3. #3
    VBAX Newbie
    Joined
    Oct 2018
    Posts
    2
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    Welcome to the forum!

    'ni = PartDocs(i).GetNumPages()
    ni = 1
    That didn't work. It still creates a PDF that has the same total number of pages.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I fiddled with it but I don't see a way to do it easily. One can split the file(s) and then merge what is needed. Seems like a lot of trouble to me.

Tags for this Thread

Posting Permissions

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