Sub MergePDF()
'Combined multiple PDF files into one
'set a reference to Acrobat (Adobe Acrobat 7.0 Type Library)
'[URL unfurl="true"]http://www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/[/URL]
'30-JUL-2010
'May need to see if file exists when this is run more than once. Will add if needed
Dim AcroApp As Acrobat.CAcroApp
Dim Part1Document As Acrobat.CAcroPDDoc
Dim Part2Document As Acrobat.CAcroPDDoc
Dim numPages As Integer
Dim pdfsrc As String
Dim X As Integer
Dim stMergeName As String
Dim strundate As String
Set AcroApp = CreateObject("AcroExch.App")
Set Part1Document = CreateObject("AcroExch.PDDoc")
Set Part2Document = CreateObject("AcroExch.PDDoc")
pdfsrc = "c:\reports\PDFfiles\Report0.pdf"
'grab run date off of form for use in the file name
'strundate = DLookup("rundate", "tbldetail", "format(rundate,'mmmyy')=Left([Forms]![frmMain].[txtCuryr],3) & Mid([Forms]![frmMain].[txtCuryr],5,2)")
X = 1
Part1Document.Open (pdfsrc)
Part2Document.Open (Replace(pdfsrc, "0", X))
Do While X < 8
' Insert the pages of Part2 after the end of Part1
numPages = Part1Document.GetNumPages()
If Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = False Then
MsgBox "Cannot insert pages"
End If
X = X + 1
' Next line is a conditional if you want to exclude certain pdf files from being combined. It is commented it out.
' If X = 2 And Forms!frmmain.txtSemester = "Sprg" Then X = 6 'Exclude SAT Pages from Spring Report
Part2Document.Close
Part2Document.Open (Replace(pdfsrc, "0", X))
'Debug.Print (Replace(pdfsrc, "0", x))
Loop
'This part changes the file name and date. Can comment out if your file name convention is different.
If Forms!frmmain.txtSemester = "Fall" Then stTerm = "F" Else stTerm = "S"
stMergeName = Replace(pdfsrc, "Report0", "Report" & Right(Me.txtCurrentYr, 2) & stTerm & "_" & Format(Me.txtRunDate, "YYYYMMDD") & "")
If Part1Document.Save(PDSaveFull, stMergeName) = False Then
MsgBox "Cannot save the modified document"
End If
Part1Document.Close
Part2Document.Close
AcroApp.Exit
Set AcroApp = Nothing
Set Part1Document = Nothing
Set Part2Document = Nothing
' This renames one of the files unmerged files, was used for another purpose, but left code here in case it is of use
' FileCopy stmergename, "c:\reports\pdffiles\stMergeName, "Full", "")
MsgBox "Merge is Done"
End Sub