How to Count the Page Numbers of Pdf Files in Excel?


You've come to the correct place if you've ever needed to figure out how many pages there are in a PDF file and wanted an easy way to track this information in Excel. This article will teach you how to use Excel's robust features to extract and record the page numbers of PDF files in a quick and efficient manner. Having a rapid and automated approach to count the page numbers can be very helpful for anyone working with huge amounts of PDF files, whether they are researchers, professionals, or students. We'll use Excel to automate this procedure rather than manually reading through each PDF file and counting the pages.

This tutorial will walk you through each step, from setting up the Excel worksheet and installing the appropriate software to extracting and storing the page numbers from various PDF files. By the end, you'll have an Excel spreadsheet that can retrieve and update your PDF documents' page counts automatically. So let's get started and see how to use Excel to count the pages in PDF files. Prepare to streamline your processes and increase production!

Count the Page Numbers of Pdf Files

Here we will first insert a VBA module and then run it to complete the task. So let us see a simple process to learn how you can count the page numbers of PDF files in Excel.

Step 1

Consider any Excel sheet.

First, right-click on the sheet name and select View Code to open the VBA application.

Right click > View code.

Step 2

Then click on Insert and select Module, then copy the below code into the text.

Insert > Module > Copy.

Code

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

Step 3

Then click F5 to run the module, select the folder containing the pdf files, and click Ok.

F5 > Select Folder > Ok.

Then you can see that page numbers will be displayed along with the PDF names. This is how you can count the page numbers of PDF files in Excel.

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can count the page numbers of PDF files in Excel to highlight a particular set of data.

Updated on: 22-Aug-2023

707 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements