AcroPDPage.GetSize Fails at Random places in Large Volume Processes
Hello - I'm developing an application in VBA to return a page level report including the X and Y values from the MediaBox struct on large volumes of PDFs for organizing by page size for printing.
My program functions perfectly on smaller data sets.
The issue I am having is that after ~4000 - 5000 pages I receive an runtime error stating that I have not SET an object correctly, which I find odd as the program worked perfectly for the large bit it got through. This feels like a memory issue, which I have tried to solve using AcroApp.Exit. However, I cannot seem to get AcroApp.Exit to return true.
My File Listing and AcroPDDoc.GetNumPages sub has retrieved
As an example, when I run this code in a module by itself, ExitAcroVar begins as false and remains false while CloseAllDocsVar changes from False to True.
Sub TestAcro()
Dim pdfApp As AcroApp
Dim pdfDoc As AcroPDDoc
Dim ExitAcroVar As Boolean
Dim CloseAllDocsVar As Boolean
Set pdfApp = CreateObject("AcroExch.app")
Set pdfDoc = CreateObject("AcroExch.PDDoc")
CloseAllDocsVar = pdfApp.CloseAllDocs()
ExitAcroVar = pdfApp.Exit()
End Sub
To take a few steps back, in the immediate usecase for the larger program, I am first creating a sheet of ~7,000 files (QCSheet = Folder, Filename, PageCount) using the Dir function in a DoWhile loop and the below Acrobat functions:
Do While xFileName <> ""
Cells(RowDocs, 1) = xFdItem
Cells(RowDocs, 2) = xFileName
pdfDoc.Open (xFdItem & xFileName)
//I am opening the PDF here because I want the pages during this initial process to create a progress bar of //sorts in the second process, but I could relegate this to the second sub.
NumPages = pdfDoc.GetNumPages
Cells(RowDocs, 3) = NumPages
PDDocCloseVar = pdfDoc.Close
RowDocs = RowDocs + 1
xFileName = Dir
Then, using those pathfilenames, I am opening the PDFs again to loop through the pages in each doc and use the AcroPDPage.GetSize function.
For i = 2 To RowDocs
pdfDoc.Open (Sheets("QCSheet").Cells(i, 1) & Sheets("QCSheet").Cells(i, 2))
For j = 0 To Sheets("QCSheet").Cells(i, 3) - 1
Cells(RowPages, 1) = Sheets("QCSheet").Cells(i, 1)
Cells(RowPages, 2) = Sheets("QCSheet").Cells(i, 2)
Cells(RowPages, 3) = Sheets("QCSheet").Cells(i, 3)
Set PDPage = pdfDoc.AcquirePage(j)
Set XYZ = PDPage.GetSize
Cells(RowPages, 4) = j + 1
Cells(RowPages, 5) = XYZ.x / 72 <- the documentation really buries the lead on how to use this stucture...
Cells(RowPages, 6) = XYZ.y / 72
RowPages = RowPages + 1
Next j
//Here is where I would like to add an If i Mod 1000 Then AcroApp.Exit...
PDDocCloseVar = pdfDoc.Close
Next i
Any help would be greatly appreciated!
Thank you,
Dean
