Get PDF Page Count

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Trying to get page count for all pdf files.
My current code at the bottom.
Error here :
Dim Ac_Fi As Acrobat.AcroPDDoc
Error message:
Compile error:
User-defined type not defined

I set references to all of these libraries I see available that appear to be Adobe related libraries:
  • Acrobat Access 3.0 Type Library
  • Adobe Acrobat Browser Control Type Library 1.0
  • AcroBrokerLib
I only have Adobe Acrobat Reader DC and Able2Extract from InvestInTech.

Or do you have other suggestions? I saw some threads suggesting to use RexEx to read the file and parse to get the page count, however I don't think that will give good results for my files due to mix of digital and scanned files.

thanks
-w

code:
VBA Code:
Option Explicit
Sub Get_Page_Count()

    'Objects
        Dim wb As Workbook
        Dim wsFolders As Worksheet
        Dim wsPageCount As Worksheet
        Dim FSO As Object
        Dim F_Fol As Object
        Dim F_File As Object
        Dim Ac_Fi As Acrobat.AcroPDDoc
    
    'Variables
        Dim T_Str As String
        Dim i As Long
        Dim r As Long
        Dim j As Long

    'Excel enrionment - speed things up
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
            .Calculation = xlCalculationManual
        End With

    'Initalize objects
        Set wb = ThisWorkbook
        With wb
            Set wsFolder = .Worksheets("Folders")
            Set wsPageCount = .Worksheets("PageCount")
        End With
        Set FSO = CreateObject("Scripting.FileSystemObject")
        
    'Initialize variables
        j = 2   'Begin output
        r = GetRows(ws:=ws, _
                    col:=2) 'Max rows list of folders and subfolders


    'Loop folders and subfolders
    'Get file name and page count
        For i = 2 To r
            Set F_Fol = FSO.getfolder(ws.Cells(i, 2).Value)  'Folders are listed in Col 2 on worksheet "Folders"
        
            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
                    wsPageCount.Cells(j, 1).Value = T_Str
                    wsPageCount.Cells(i, 2).Value = Ac_Fi.GetNumPages
                    j = j + 1
                    Ac_Fi.Close
                    Set Ac_Fi = Nothing
                    Next j
                End If
            Next F_File

'Tidy Up
    'Destroy objects
        Set F_File = Nothing
        Set F_Fol = Nothing
        Set FSO = Nothing
        Set wsFolder = Nothing
        Set wsPageCount = Nothing
        Set wb = Nothing

    'Excel enrionment - restore
        With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With

End Sub

Functions:
Code:
Public Function GetRows(ws As Worksheet, _
                        col As Long) As Long
filesys
    'Input          :   ws  : a Worksheet
    '               :   col : Column Number
    'Output         :   A row number of type long
    
    'Get last row of column
        With ws
            GetRows = .Cells(Rows.Count, col).End(xlUp).Row
        End With
        
End Function
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top