VBA Merge PDF's based on path

Daccota_lenny

New Member
Joined
Oct 13, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello!

I need help getting VBA to Merge PDF's based on the path. I've attached a photo. Column A, B, C are file paths as they currently exist. I need VBA to grab and merge all of these columns files and use the Output location in column D as the reference for the new path name and where it should be saved/named. I have Acrobat Pro and Office 365.

EDIT: I should also note that this is just an example, the PDFs being merged will be hundreds of rows so the code would need to be able to move down to the next row, execute the task and so forth.
 

Attachments

  • Path lineup.PNG
    Path lineup.PNG
    15.1 KB · Views: 81

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try the code at


which merges 2 input PDFs specified in columns A and B to create 1 output PDF specified in column C. You would need to modify the code to handle 3 input PDFs specified in columns A to C and the output PDF in D.
 
Upvote 0
Public Sub Merge_PDFs()

Dim PDFfiles As Variant
Dim i As Long
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc

With ActiveSheet
PDFfiles = .Range("A2", .Cells(.Rows.Count, "C").End(xlUp)).Value
End With

'Create Acrobat API objects

Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")

'Loop through rows, open PDF file in column A, open and insert PDF file in column B, save as PDF file in column C

For i = 1 To UBound(PDFfiles)
objCAcroPDDocDestination.Open PDFfiles(i, 1)
objCAcroPDDocSource.Open PDFfiles(i, 2)
If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
MsgBox "Error merging" & vbCrLf & PDFfiles(i, 1) & vbCrLf & "and" & vbCrLf & PDFfiles(i, 2), vbExclamation
End If
objCAcroPDDocSource.Close
objCAcroPDDocDestination.Save 1, PDFfiles(i, 3)
objCAcroPDDocDestination.Close
Next

Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing

MsgBox "Done"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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