top of page

Yet Another VBA Code to Get Page Counts for Multiple PDF files

Kenneth Hobs has posted VBA code to this site, which is a workable alternative to the VBA code discussed in the Tips of the Night for April 11, 2017 and April 13, 2017. that allow you generate a list of the page totals of multiple PDFs.

In order to get this code to work, you'll need to select the Acrobat reference library from the Tools menu in Visual Basic.

Put the code in a new module in Visual Basic and simply press play. It's not necessary to enter the path containing your PDF files in the code, the code itself will prompt you to select a folder.

The code will generate a list like the one below in a few seconds.

Sub GetPDFNumberOfPages() Dim FSO As Object Dim F_Fol As Object Dim F_File As Object Dim T_Str As String Dim Dlg_Fol As FileDialog 'In VBE, add reference: Tools > References... > Acrobat > OK Dim Ac_Fi As Acrobat.AcroPDDoc Dim i As Long

Set Dlg_Fol = Application.FileDialog(msoFileDialogFolderPicker) If Dlg_Fol.Show = -1 Then T_Str = Dlg_Fol.SelectedItems(1) Else: Set Dlg_Fol = Nothing End If Set Dlg_Fol = Nothing Set FSO = CreateObject("Scripting.FileSystemObject") Set F_Fol = FSO.getfolder(T_Str) i = 2 For Each F_File In F_Fol.Files T_Str = UCase(F_File.Path) If Right(T_Str, 4) = ".PDF" Then Set Ac_Fi = New Acrobat.AcroPDDoc Ac_Fi.Open T_Str Cells(i, 1).Value = T_Str Cells(i, 2).Value = Ac_Fi.GetNumPages i = i + 1 Ac_Fi.Close Set Ac_Fi = Nothing End If Next


Set F_File = Nothing Set F_Fol = Nothing Set FSO = Nothing End Sub

bottom of page