Skip to main content

How to count the page numbers of Pdf files in Excel?

If there are multiple Pdf files in a specific folder, now, you want to display all these file names in a worksheet, and get the page numbers of each file. How could you deal with this job in Excel quickly and easily?

Count the page numbers of Pdf files from a folder in worksheet with VBA code


Count the page numbers of Pdf files from a folder in worksheet with VBA code

May be the following VBA code can help you to display all Pdf file names and their each page numbers in a worksheet, please do as this:

1. Open a worksheet where you want to get the Pdf files and page numbers.

2. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

3. Click Insert > Module, and paste the following macro in the Module Window.

VBA code: List all Pdf file names and page numbers in worksheet:

Sub Test()
    Dim I As Long
    Dim xRg As Range
    Dim xStr As String
    Dim xFd As FileDialog
    Dim xFdItem As Variant
    Dim xFileName As String
    Dim xFileNum As Long
    Dim RegExp As Object
    Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
    If xFd.Show = -1 Then
        xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
        xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
        Set xRg = Range("A1")
        Range("A:B").ClearContents
        Range("A1:B1").Font.Bold = True
        xRg = "File Name"
        xRg.Offset(0, 1) = "Pages"
        I = 2
        xStr = ""
        Do While xFileName <> ""
            Cells(I, 1) = xFileName
            Set RegExp = CreateObject("VBscript.RegExp")
            RegExp.Global = True
            RegExp.Pattern = "/Type\s*/Page[^s]"
            xFileNum = FreeFile
            Open (xFdItem & xFileName) For Binary As #xFileNum
                xStr = Space(LOF(xFileNum))
                Get #xFileNum, , xStr
            Close #xFileNum
            Cells(I, 2) = RegExp.Execute(xStr).Count
            I = I + 1
            xFileName = Dir
        Loop
        Columns("A:B").AutoFit
    End If
End Sub

4. After pasting the code, and then press F5 key to run this code, and a Browse window is popped out, please select the folder that contains the Pdf files you want to list and count page numbers, see screenshot:

doc count pdf pages 1

5. And then, click OK button, all Pdf file names and page numbers are listed into the current worksheet, see screenshot:

doc count pdf pages 2

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (79)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Thank you so much for the code
This comment was minimized by the moderator on the site
Is it possible to have it itemize instead of group these page numbers. For example if PDF 1 has 3 pages and PDF 2 has 2 pages, how do i get it to look like this?

File Name Page
PDF 1 1
PDF 1 2
PDF 1 3
PDF 2 1
PDF 2 2
This comment was minimized by the moderator on the site
This is so amazing and helpful. Is there a way to run this in a folder that has several subfolders and do all the files at once instead of having to run it for each folder?
This comment was minimized by the moderator on the site
Hello, MWilkers

You can view this comment to solve your problem:
https://www.extendoffice.com/documents/excel/5330-excel-vba-pdf-page-count.html#comment-39782,32012,20

Please have a try.
This comment was minimized by the moderator on the site
Thank you, the code worked very well. Is it possible to get the last modified date of PDF file as well in a separate column
This comment was minimized by the moderator on the site
Thank you for the code.
This comment was minimized by the moderator on the site
Wow, this has been SO helpful to me. Thank you!
This comment was minimized by the moderator on the site
Buenas. Me funciona perfecto. Ahora lo que necesitaría es imprimir unas paginas en concreto y llamo a esta rutina pero no imprime el pdf sino el excel.
Sub imprimirpdf()
Dim rutaPDF As String
rutaPDF = Sheets("Hoja1").Range("D2")
F = Cells(2, 7)
T = Cells(2, 8)
pid = Shell("C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe " & Chr(34) & rutaPDF & Chr(34))
ActiveSheet.PrintOut From:=F, To:=T
DoEvents 'Paso 5 - Esperar que se Imprima el PDF
hnd = OpenProcess(PROCESS_TERMINATE, True, pid) 'Paso 6 - Obtener el handle(manejador) del proceso(Adobe Reader)
TerminateProcess hnd, 0 '
End Sub
This comment was minimized by the moderator on the site
Hallo vielen Dank für den Code und die super Erklärung
Ich habe nun das Problem das meine PDF-Dateien zwei unterschiedliche Seitengröße (A4 und A6) inne haben.
Nun würde ich gerne Pro PDF diese unterschied zusätzlich auslesen können.
Habe Sie hierfür vielleicht eine Lösung.

Gerne können sie mich auch unter email: erreichen.

MFG,
Sebastian
This comment was minimized by the moderator on the site
I get 0 pages for most pdfs, help?
This comment was minimized by the moderator on the site
Hello, MAXMAN
Could you upload the PDF file which can't get the correct number here? Or you can send your PDF file to my email: .
So that we can check the problem.
Thank you!
This comment was minimized by the moderator on the site
thanks
but i get an error runtime error "52" bad file name or number
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations