Search

VBA to combine PDFs into one PDF file




VBA to combine PDFs into one PDF file
Here's free e-course of PDF Automation using VBA: link


Option Explicit
Sub CombinePDFsTest()
Dim lr&, j&, myArrFiles() As String

lr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 'A as primary key
ReDim myArrFiles(lr) As String

For j = 2 To lr
   myArrFiles(j - 2) = Sheets("Sheet1").Cells(j, 1)
Next

Call CombinePDFs(myArrFiles, "C:\Users\M515269\Desktop\final\existingFileName_toCombineHere.pdf", _
   "C:\Users\M515269\Desktop\final\final_combined_file_name.pdf")

End Sub

Public Function CombinePDFs(arrFiles() As String, path_docPC_combineTo As String, path_saveAs_final As String) As Boolean

Dim DestinationPDF As Acrobat.CAcroPDDoc
Dim SourcePDF As Acrobat.CAcroPDDoc
Dim docpc_final As Acrobat.CAcroPDDoc


Dim i As Integer
Dim failedCount As Integer

On Error GoTo AcrobatAPI_Missing:

Set DestinationPDF = CreateObject("AcroExch.PDDoc")
Set SourcePDF = CreateObject("AcroExch.PDDoc")
Set docpc_final = CreateObject("AcroExch.PDDoc")

docpc_final.Open path_docPC_combineTo
    For i = LBound(arrFiles) + 1 To UBound(arrFiles)
SourcePDF.Open (arrFiles(i))
If docpc_final.InsertPages(docpc_final.GetNumPages - 1, SourcePDF, 0, SourcePDF.GetNumPages, 0) Then CombinePDFs = True Else failedCount = failedCount + 1 End If SourcePDF.Close Next i docpc_final.Save 1, path_saveAs_final docpc_final.Close Set SourcePDF = Nothing Set docpc_final = Nothing AcrobatAPI_Missing: If failedCount <> 0 Then CombinePDFs = False End If On Error GoTo 0 End Function

No comments:

Post a Comment

Note: only a member of this blog may post a comment.