Combine multiple PDF files with VBA

EvdM

New Member
Joined
Dec 2, 2020
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Dear VBA Specialists,

I want to create a macro which merges multiple PDF files, but the amount of files that has to be merged is variable and need to be determined in the excelsheet. So my wish is to gain a VBA code that reads the filenames out of a folder, Somehow select which ones should merged into the same file and let Adobe Acrobat Pro do the rest.

I have achieved to get a code running that reads out a folder and projets the filename and path in the sheet (column A and B). Now should be selected which files belong in the same combined PDF. My best guess is to write down the name of the future merged filename in column C...? And adjust the code to that system. (Example Below)


Filepath 1File 1Mergedfile 1
Filepath 2File 2Mergedfile 1
Filepath 3File 3Mergedfile 1
Filepath 4File 4Mergedfile 2
Filepath 5File 5Mergedfile 2

Online are some examples of VBA code that could merge PDF files but not exactly working like how I imagine.
This raises the question if it makes sense to let it work how I described over here?

If you can have any advice or can help me out with making this work, that would be very much appreciated!

Kind regards,

Edwin
 
now i happened an issue but i don't know what is reason, Could you help me to resolve this case?

As I posted above:
I should have seen that Acrobat.PDSaveFlags.PDSaveFull would require a reference to the Adobe Acrobat Type library. Apart from that, the code uses late binding of the Acrobat objects and therefore you can replace that enumeration name with the value 1 and remove the library reference.

Therefore change the errant line to:
VBA Code:
        PDDocDestination.Save 1, PDFfiles(i, 1) & PDFfiles(i, 3)
and it should compile successfully.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
As I posted above:


Therefore change the errant line to:
VBA Code:
        PDDocDestination.Save 1, PDFfiles(i, 1) & PDFfiles(i, 3)
and it should compile successfully.
Dear Mr John
thank for your support, i tried change the code as you instruction, but now i had new issue as picture attachment, please help me to fix this
thank you!
1650357916739.png
 
Upvote 0
You haven't shown the actual error caused by the highlighted line. Have you installed Acrobat Pro? The OP has, therefore the code can use the Acrobat API.
Dear Mr John
Many thanks!
i combined success after reinstall Acrobat pro

thank you so much!!!
 
Upvote 0
Hi sir,

Have you checked for Tools --> Reference, if Acrobat is selected?
 
Upvote 0
Welccome to MrExcel forums.

Try this macro:
VBA Code:
Option Explicit

Public Sub Merge_PDFs()

    Dim PDFfiles As Variant
    Dim i As Long
    Dim PDDocDestination As Object 'Acrobat.CAcroPDDoc
    Dim PDDocSource As Object 'Acrobat.CAcroPDDoc
      
    With ActiveSheet
        PDFfiles = .Range("A2", .Cells(.Rows.Count, "C").End(xlUp)).Value
    End With
  
    'Create Acrobat API objects
  
    Set PDDocDestination = CreateObject("AcroExch.PDDoc")
    Set PDDocSource = CreateObject("AcroExch.PDDoc")
  
    'Loop through rows, opening PDF file in column A & B, merging to and saving as PDF file in column A & C
  
    For i = 1 To UBound(PDFfiles)
        If Right(PDFfiles(i, 1), 1) <> "\" Then PDFfiles(i, 1) = PDFfiles(i, 1) & "\"
        If Dir(PDFfiles(i, 1) & PDFfiles(i, 3)) = vbNullString Then
            'Destination (merged) PDF doesn't exist, so create new file
            PDDocDestination.Create
        Else
            'Open existing destination (merged) PDF
            PDDocDestination.Open PDFfiles(i, 1) & PDFfiles(i, 3)
        End If
        PDDocSource.Open PDFfiles(i, 1) & PDFfiles(i, 2)
        If Not PDDocDestination.InsertPages(PDDocDestination.GetNumPages - 1, PDDocSource, 0, PDDocSource.GetNumPages, 0) Then
            MsgBox "Error merging" & vbCrLf & PDFfiles(i, 1) & PDFfiles(i, 2) & vbCrLf & "to" & vbCrLf & PDFfiles(i, 1) & PDFfiles(i, 3), vbExclamation
        End If
        PDDocSource.Close
        PDDocDestination.Save Acrobat.PDSaveFlags.PDSaveFull, PDFfiles(i, 1) & PDFfiles(i, 3)
        PDDocDestination.Close
    Next
  
    Set PDDocSource = Nothing
    Set PDDocDestination = Nothing

    MsgBox "Done"
  
End Sub
Hi John ,

When i am trying this code i am getting error . any help in this?


1657807083856.png


1657807126875.png
 
Upvote 0
Thank You , John for responding.

Actually we have PDF X-change Editor as well as PDF X-Change pro in the office

When I change code from AcroExch.App to AcroExch.PDDoc , it does not give error , but stuck at "PDDocdestination . Create ", as Create method is not available with AcroExch.APP
 
Upvote 0
and Yes John , if this class do not support PDF X-change Editor as well as PDF X-Change pro , Is there any class of AcroExch that we can use and sort out the issue , as PDF X-change Editor as well as PDF X-Change pro , both having capabilities of Combining PDF as well as insering , deleting etc.

Thanks Once again.
 
Upvote 0

Forum statistics

Threads
1,215,340
Messages
6,124,382
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top