Option Explicit
Public Sub Merge_All_School_PDFs3()
Dim PDFsTable As ListObject
Dim headerSheet As Worksheet
Dim PDFsFolder As String, inputPDFs As String, FinalMergedPDF As String
Dim i As Long, n As Long
Dim school As String
Dim numPages As Long
Dim command As String
Dim MergeNumber As Long, MergedPDF As String
Dim PDFsColl As Collection, PDFfile As Variant
Dim Wsh As Object
Const MAX_COMMAND_LENGTH = 3000
Set Wsh = CreateObject("WScript.Shell")
Set PDFsTable = ActiveSheet.ListObjects(1)
Set headerSheet = ThisWorkbook.Worksheets.Add(Before:=ThisWorkbook.Worksheets(1))
With PDFsTable
PDFsFolder = Left(.DataBodyRange(1, 2).Value, InStrRev(.DataBodyRange(1, 2).Value, "\"))
FinalMergedPDF = PDFsFolder & "All Schools Merged.pdf"
Wsh.Run "cmd /c DEL " & Q2(PDFsFolder & "MERGE_*.pdf"), 0, True
If Dir(FinalMergedPDF) <> vbNullString Then Kill FinalMergedPDF
headerSheet.Range("A1").ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFsFolder & "BLANK.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Set PDFsColl = New Collection
school = ""
For i = 1 To .DataBodyRange.Rows.Count
Debug.Print .DataBodyRange(i, 1).Value, .DataBodyRange(i, 2).Value, .DataBodyRange(i, 3).Value
If .DataBodyRange(i, 1).Value <> school Then
school = .DataBodyRange(i, 1).Value
With headerSheet.Range("C25")
.Clear
.Value = school
.Font.Name = "Calibri"
.Font.Size = 72
.Font.Bold = True
End With
headerSheet.Range("A1:I60").ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFsFolder & school & " HEADER.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
PDFsColl.Add school & " HEADER.pdf"
End If
numPages = GetNumPages2(.DataBodyRange(i, 2).Value)
For n = 1 To .DataBodyRange(i, 3).Value
PDFsColl.Add Mid(.DataBodyRange(i, 2).Value, InStrRev(.DataBodyRange(i, 2).Value, "\") + 1)
If numPages Mod 2 <> 0 Then
PDFsColl.Add "BLANK.pdf"
End If
Next
Next
End With
MergeNumber = 1
MergedPDF = "MERGE_" & MergeNumber & ".pdf"
inputPDFs = ""
For Each PDFfile In PDFsColl
If Len("CD /D " & Q2(PDFsFolder) & " & PDFtk " & inputPDFs & Q2(PDFfile) & " " & "cat output " & Q2(MergedPDF)) < MAX_COMMAND_LENGTH Then
inputPDFs = inputPDFs & Q2(PDFfile) & " "
Else
command = "CD /D " & Q2(PDFsFolder) & " & PDFtk " & inputPDFs & "cat output " & Q2(MergedPDF)
Debug.Print command
Wsh.Run "cmd /c " & command, 0, True
inputPDFs = Q2(MergedPDF) & " " & Q2(PDFfile) & " "
MergeNumber = MergeNumber + 1
MergedPDF = "MERGE_" & MergeNumber & ".pdf"
End If
Next
If inputPDFs <> "" Then
command = "CD /D " & Q2(PDFsFolder) & " & PDFtk " & inputPDFs & "cat output " & Q2(MergedPDF)
Debug.Print command
Wsh.Run "cmd /c " & command, 0, True
End If
FileCopy PDFsFolder & MergedPDF, FinalMergedPDF
Application.DisplayAlerts = False
headerSheet.Delete
Application.DisplayAlerts = True
Wsh.Run "cmd /c DEL " & Q2(PDFsFolder & "MERGE_*.pdf"), 0, True
Wsh.Run "cmd /c DEL " & Q2(PDFsFolder & "* HEADER.pdf"), 0, True
Wsh.Run "cmd /c DEL " & Q2(PDFsFolder & "BLANK.pdf"), 0, True
MsgBox "Created " & FinalMergedPDF, vbInformation
End Sub
Private Function Q2(text As Variant) As String
Q2 = Chr(34) & text & Chr(34)
End Function
Private Function GetNumPages2(PDFfullName As String) As Long
Dim Wsh As Object
Dim FSO As Object
Dim ts As Object
Dim dataFile As String
Dim command As String
Dim allText As String
dataFile = Left(PDFfullName, InStrRev(PDFfullName, "\")) & "dump_data.txt"
command = "PDFtk " & Q2(PDFfullName) & " dump_data output " & Q2(dataFile)
Debug.Print command
Set Wsh = CreateObject("WScript.Shell")
Wsh.Run "cmd /c " & command, 0, True
Set FSO = CreateObject("Scripting.FileSystemObject")
Set ts = FSO.OpenTextFile(dataFile, 1)
allText = ts.ReadAll
ts.Close
FSO.DeleteFile dataFile
GetNumPages2 = Split(Split(allText, "NumberOfPages: ")(1), vbCrLf)(0)
Debug.Print GetNumPages2 & " pages"
End Function