Sub Test_MergePDFs()
Dim a As Variant
a = WorksheetFunction.Transpose(Range("A2:A" & Range("A2").End(xlDown).Row).Value)
MergePDFs Range("C2").Value2, Join(a, ","), Range("B2").Value2
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
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Adjust MyPath string if needed.
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
' Save to MyPath folder if target folder for merged PDF file was not input. (ken)
If InStr(DestFile, "\") = 0 Then DestFile = p & DestFile
On Error GoTo exit_
If fso.FileExists(p & DestFile) Then Kill p & DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Not fso.FileExists(p & a(i)) Then
Debug.Print "File not found" & vbLf & p & a(i)
GoTo ElseI
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & a(i)
ni = PartDocs(i).GetNumPages()
If i > 0 And ni > 0 Then
' Merge PDF to PartDocs(0) document
If Not PartDocs(0).InsertPages(n, PartDocs(i), 0, ni, True) Then
Debug.Print "Cannot insert pages of" & vbLf & p & a(i)
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
ElseI:
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next i
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & 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 was created in:" & 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
Set fso = Nothing
End Sub