I am also trying to research this question online. I found a post on another forum that provided the code below. Do you see this code as meeting my stated needs?
Public Sub mergePDFFiles(inDirectory As String, outMergeFile As String, fileList() As String)
Dim AcroApp As CAcroApp
Dim PDDoc As CAcroPDDoc
Dim InsertPDDoc As CAcroPDDoc
Dim iNumberOfPagesToInsert As Integer, iLastPage As Integer
'On Error GoTo ErrorHandler
Set AcroApp = CreateObject("AcroExch.App")
Set PDDoc = CreateObject("AcroExch.PDDoc")
Set InsertPDDoc = CreateObject("AcroExch.PDDoc")
'/ Is there a trailing backslash on the inDirectory?
If Right(inDirectory, 1) <> "" Then
inDirectory = inDirectory & ""
End If
'/ Hide the Acrobat window
AcroApp.Hide
'/ Create a Blank PDDoc (The Merge File) - Alterations courtesy of KDA.
If PDDoc.Create = False Then
MsgBox "Creation not successful"
End
End If
'/PDDoc.Save &H1, inDirectory & outMergeFile
For goRound = LBound(fileList()) To UBound(fileList())
' Get the total pages less one for the last page num [zero based]
iLastPage = PDDoc.GetNumPages - 1
'/If (iLastPage = -1) Then iLastPage = 0
If fileList(goRound) = "" Then
Exit For
End If
'/ Using the fileList Array access the first index member
If InsertPDDoc.Open(fileList(goRound)) = False Then
writeError inDirectory & "errpdfMerge.log", "Error Opening The File To Insert - " & fileList(goRound)
'/ MsgBox "Error Opening The File To Insert - " & fileList(goRound), , "MergePDFs - Command Line"
Exit Sub
End If
'/ Get the number of pages to insert from the Current InsertPDDoc object
iNumberOfPagesToInsert = InsertPDDoc.GetNumPages
strOutput = "Go Round Counter for fileList array: " & goRound & vbCrLf & _
"File List Array Item: " & fileList(goRound) & vbCrLf & _
"Insert Document: " & InsertPDDoc.GetFileName & vbCrLf & _
"Last Page Number: " & iLastPage & vbCrLf & _
"Number of Pages to Insert: " & iNumberOfPagesToInsert & vbCrLf & _
"File Name of PDDoc: " & PDDoc.GetFileName & vbCrLf & _
"Directory of In Files: " & inDirectory & vbCrLf & _
"Title from Insert Doc: " & InsertPDDoc.GetInfo("Title") & vbCrLf & _
"Number of Files in Array: " & cntFiles
'/MsgBox strOutput
'/ Insert the pages
If PDDoc.InsertPages(iLastPage, InsertPDDoc, 0, iNumberOfPagesToInsert, True) = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Inserting Pages - " & inDirectory & fileList(goRound)
'/MsgBox "Error Inserting Pages - " & inDirectory & fileList(goRound), , "MergePDFs - Command Line"
Exit Sub
End If
' Close the document without saving
If InsertPDDoc.Close() = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Closing Insert Document - " & fileList(goRound)
'/MsgBox "Error Closing Insert Document - " & fileList(goRound), , "MergePDFs - Command Line"
Exit For
End If
writeLog inDirectory & "pdfMerge.log", inDirectory & fileList(goRound), iNumberOfPagesToInsert
Next
'/ Save the File Optimized
If (PDDoc.Save(&H5, outMergeFile)) = False Then
' writeError inDirectory & "errpdfMerge.log", "Error Saving The Merged Document - " & outMergeFile
MsgBox "Error Saving The Merged Document - " & outMergeFile, , "MergePDFs - Command Line"
Exit Sub
End If
' Close the PDDoc
If PDDoc.Close() = False Then
'writeError inDirectory & "errpdfMerge.log", "Error Closing The Merged Document - " & fileList(goRound)
MsgBox "Error Closing The Merged Document - " & fileList(goRound), , "MergePDFs"
Exit Sub
End If
' Close Acrobat Exchange
AcroApp.Exit
Set AcroApp = Nothing
Set PDDoc = Nothing
Set InsertPDDoc = Nothing
'/MsgBox "Merge Completed", vbOKOnly, "MergePDF's - Command Line Version"
'writeLogFooter inDirectory & "pdfMerge.log"
Exit Sub
ErrorHandler:
MsgBox Err.Description & " dave", , "MergePDFs"
Err.Clear
Exit Sub
End Sub
Public Sub writeLog(logFileName As String, fileBeingInserted As String, numberOfPagesInserted As Integer)
Open logFileName For Append As #3
Print #3, fileBeingInserted & "," & numberOfPagesInserted
'Print #1, "--------------- end of session: " & Now()
Close #3
End Sub
Bookmarks