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:\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 < Forms!frmDashboard!CountReports
' 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
'Close the open pdf files except for the Merged report
' FollowHyperlink Replace(pdfsrc, "0", X - 1), , True, False
' SendKeys "%{F4}", False
' DoEvents
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", "MergedReports")
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:\stMergeName, "Full", ""
MsgBox "Reports merged successfully", vbInformation, "Merge Successful"