Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 49

Thread: Need code to merge PDF files in a folder using adobe acrobat X

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

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

    Hi,

    I need a code to merge all the PDF files in a folder using adobe acrobat X full version. I will provide the link to the folder in Range E3. Can anybody help me with this.

    Thanks

  2. #2
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Hi,
    Set reference to VBE - Tools - References – Acrobat
    and try this code:
    Sub MergePDFs()
    ' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
    ' Reference required: "VBE - Tools - References - Acrobat"
     
      ' --> Settings, change to suit
      Const MyPath = "C:\Temp"            ' Path where PDF files are stored
       Const MyFiles = "1.pdf,2.pdf,3.pdf"  ' List of PDFs to ne merged
      Const DestFile = "MergedFile.pdf"   ' The name of the merged file
      ' <-- End of settings
     
      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
    Last edited by ZVI; 08-27-2013 at 05:19 AM.

  3. #3
    Hi thanks for the code. Only one problem in Const MyFiles i cannot name all the PDF files because there are many files and will change every time. Can this be done in such a way that all the pdf files in the folder will be merged.

  4. #4
    VBAX Regular
    Joined
    Dec 2008
    Posts
    86
    Location

    Exclamation

    Quote Originally Posted by ZVI View Post
    Hi,
    Set reference to VBE - Tools - References – Acrobat
    and try this code:
    Sub MergePDFs()
    ' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
    ' Reference required: "VBE - Tools - References - Acrobat"
     
      ' --> Settings, change to suit
      Const MyPath = "C:\Temp"            ' Path where PDF files are stored
       Const MyFiles = "1.pdf,2.pdf,3.pdf"  ' List of PDFs to ne merged
      Const DestFile = "MergedFile.pdf"   ' The name of the merged file
      ' <-- End of settings
     
      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
    Hi there!
    Despite setting the correct references, I cannot get this working.
    The problem lies in this line
    Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
    AcrobatError.PNGIt says "Activex component can't create object"

    Any thoughts?

    /Kish

  5. #5
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by kishlaya View Post
    Hi there!
    Despite setting the correct references, I cannot get this working.
    The problem lies in this line
    Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
    It says "Activex component can't create object"

    Any thoughts?

    /Kish
    Hi,

    Try using early binding: Set PartDocs(i) = New Acrobat.AcroPDDoc
    Also follow the recommendations of the post #10

    Regards
    Last edited by ZVI; 08-21-2014 at 07:26 AM.

  6. #6
    VBAX Regular
    Joined
    Dec 2008
    Posts
    86
    Location
    Hi,

    Sorry no luck. Still the same result.

    Regards,
    Kish

  7. #7
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by kishlaya View Post
    Sorry no luck. Still the same result.
    Hope that Excel version is not Starter Edition which has some major limitations.
    Try manually setting the reference to Acrobat X Pro library as it was recommended in post #10.
    Try reinstalling the Acrobat X Pro.

  8. #8

    Error!!! Urgent

    Hi i am getting the error of user defined type not defined. I am using Excel 2013 and the libraries available for me are
    1) Adobe Acrobat browser control type library 1.0
    2) Adobe reader file preview
    3)Acrobat access 3.0


    Capture.PNG
    Version is 15.010
    please help its very urgent
    Last edited by Shiva117; 08-18-2016 at 11:13 AM.

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    See post 34.

    You must have Acrobat installed and not just Adobe Reader.

  10. #10
    Hi,

    Can we do it using Acrobat reader. if yes please share the code. even if I need to use third part controls please help me on this

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Quote Originally Posted by Shiva117 View Post
    Hi,

    Can we do it using Acrobat reader. if yes please share the code. even if I need to use third part controls please help me on this
    Shiva117, did you not READ post #38 by Kenneth. You must have Acrobat installed and not just Adobe Reader for this code to work.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Well, then try this one:
    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")
    ' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
    ' 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
                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

  13. #13
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location

    Talking Great

    Hi,

    Can you please help me as well, I need to save all the files in a folder(pdf files)(whatever be the name) by converting them to a single file using Adobe to another folder. some of the files in the folder(which are to be converted to a single) are password protected also. please HELP, Thanks!!

  14. #14
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum! Please start your own thread. http://www.vbaexpress.com/forum/newt...newthread&f=17

    You can reference this thread if needed. If links can not be posted yet, list thread 47310 as the reference.

    I guess you want all the files merged into one file? I guess all the files have the same password? I guess that you mean Adobe Acrobat and NOT Adobe Reader?

  15. #15
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    3
    Location
    Thanks!! I have posted a new thread. And only 3 files are password protected.. I need total 5 files to be converted into one and saved to another location

  16. #16
    Thanks for the code but i am getting this error under Sub MergePDFs query (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)
    .
    Compile error: User-defined type not defined

  17. #17
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by rockybalboa View Post
    Compile error: User-defined type not defined
    As it was said in post #2 and has been mentioned in the comments, you need to set reference to VBE - Tools - References – Acrobat.
    Press Alt-F11 and choose menu Tools - References, enable checkbox with Acrobat option (may be also Adobe Acrobat ##.# Type Library, where XX.X is version number), and press OK.

  18. #18
    ohh yes, so srry. Thanks. Will try now

  19. #19
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    It's ok
    By the way, code works with free Adobe Reader because the required library is included in it.
    But if your Excel version is 32 bit then install Adobe Reader 32bit. And the same is for 64bit pair versions.

  20. #20
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    There are several PDF programs one can use to merge and splice/split PDF files. PDFSam is one. Here is a split example. http://www.vbaexpress.com/forum/show...d-instructions

    PDFSam also has a GUI method to do it as well. Before I got Adobe Pro, it used one of those methods.

    I also wrote a vb.net file to accept command line parameters to do it using the iTextSharp.dll routines.

    There are several other 3rd party PDF programs to can accept command line parameters if you are seeking that kind of solution.

Posting Permissions

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