Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何計算Excel中Pdf文件的頁數?

現在,如果特定文件夾中有多個Pdf文件,則要在工作表中顯示所有這些文件名,並獲取每個文件的頁碼。 您如何快速,輕鬆地在Excel中處理這項工作?

使用VBA代碼計算工作表中文件夾中的Pdf文件的頁碼


使用VBA代碼計算工作表中文件夾中的Pdf文件的頁碼

以下VBA代碼可能可以幫助您在工作表中顯示所有Pdf文件名及其每個頁碼,請按以下步驟操作:

1。 打開一個工作表,您要在其中獲取Pdf文件和頁碼。

2。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

3。 點擊 插入 > 模塊,然後將以下宏粘貼到 模塊 窗口。

VBA代碼:在工作表中列出所有Pdf文件名和頁碼:

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。 粘貼代碼後,然後按 F5 運行此代碼的關鍵,以及 瀏覽 彈出窗口,請選擇包含要列出的Pdf文件的文件夾併計算頁碼,請參見屏幕截圖:

doc數量pdf頁面1

5。 然後,單擊 OK 按鈕,所有Pdf文件名和頁碼都列在當前工作表中,請參見屏幕截圖:

doc數量pdf頁面2


最佳辦公效率工具

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 超過300種強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    LHH · 2 months ago
    Good day, I had the problem that for some versions of PDF with Word, this code gave me sometimes a multiple (like 4x) of the actual page numbers. My solution was to search a string in the PDF file that actually states the page numbers and if it can be of help for anyone, this is the sub I used:
    Function GetPDFpag(File1 As String) As Long

    Const ForReading = 1, ForWriting = 2
    Dim FSO As Object
    Dim FileIn, FileOut, strTmp, strOut, Scheck As String
    Dim Nstart, Nstop As Long
    Dim K As Long

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FileIn = FSO.OpenTextFile(File1, ForReading, False, 0)

    'we search for the first line with string "/Kids[" in which the number of pages is
    Scheck = "no"
    K = 1
    Do Until FileIn.AtEndOfStream Or Scheck = "yes"
    K = K + 1
    strTmp = FileIn.readline
    If Len(strTmp) > 0 Then
    If InStr(1, strTmp, "/Count", vbTextCompare) > 0 And InStr(1, strTmp, "/Kids[", vbTextCompare) > 0 Then
    strOut = strTmp
    Scheck = "yes"
    End If
    End If
    Loop

    If Scheck = "no" Then
    strOut = 0
    Else
    Nstart = InStr(strOut, "/Count") + 7
    Nstop = InStr(strOut, "/Kids")
    Nstop = Nstop - Nstart
    strOut = Mid(strOut, Nstart, Nstop)
    End If

    FileIn.Close
    'FileOut.Close

    GetPDFpag = Val(strOut)
    Set FSO = Nothing
    End Function
  • To post as a guest, your comment is unpublished.
    Robbie · 2 months ago
    Any chance this could be expanded to pull a Bates number from the first page of each pdf?
  • To post as a guest, your comment is unpublished.
    Steco · 4 months ago
    Hi Skyyang,
    First I'd like to thank you for that incredible work you do, and the time you take...
    I'm searching for a while for a VBA code :
    I Have an Excelsheet with in column "J" a list of pdf, xlsx and elm files located in a data room directory (with subdirectory's)
    File name are complete with type X:\Data_Room\Sub_directory_1\file.pdf
    The code should fill the column "I" with the number of pages of each .pdf and .xls files (no need for other, cels should stay blank)
    Could you please help me?
  • To post as a guest, your comment is unpublished.
    John · 5 months ago
    is there a way to include .doc I noticed that it works for .docx but not .doc
    • To post as a guest, your comment is unpublished.
      skyyang · 5 months ago
      Hi, John,
      To count the pages of .doc and .docx as well as the PDF files, please apply the following code:
      Sub StatisticsPage() 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 Dim xWdApp Dim xWd Set xFd = Application.FileDialog(msoFileDialogFolderPicker) If xFd.Show = -1 Then Application.ScreenUpdating = False 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 xFileName = Dir(xFdItem & "*.docx", vbDirectory) Set xWdApp = CreateObject("Word.Application") Do While xFileName <> "" Cells(I, 1) = xFileName xFileNum = FreeFile Set xWd = GetObject(xFdItem & xFileName) Cells(I, 2) = xWd.ActiveWindow.Panes(1).Pages.Count xWd.Close False I = I + 1 xFileName = Dir Loop xFileName = Dir(xFdItem & "*.doc", vbDirectory) Set xWdApp = CreateObject("Word.Application") Do While xFileName <> "" Cells(I, 1) = xFileName xFileNum = FreeFile Set xWd = GetObject(xFdItem & xFileName) Cells(I, 2) = xWd.ActiveWindow.Panes(1).Pages.Count xWd.Close False I = I + 1 xFileName = Dir Loop Columns("A:B").AutoFit End If Application.ScreenUpdating = True End Sub
      Please try, hope it can help you!

  • To post as a guest, your comment is unpublished.
    ThomasB · 6 months ago
    Hello,

    Is het possible to also get the dimensions of the pages and the creator of the pdf in this macro?

    can someone help me with this?
  • To post as a guest, your comment is unpublished.
    shivdin · 7 months ago
    Hello, this works really well thanks!, is it possible to get the page size for the first page of the PDF document?
  • To post as a guest, your comment is unpublished.
    shivdin@hotmail.com · 7 months ago
    Hello, this really works well, thank you. Is it possible to get the page size of the first page in a new column? example 8.5 x 11, 11 x 17 etc.

  • To post as a guest, your comment is unpublished.
    deepak · 8 months ago
    I have opened a pdf file who's path and name is mention in excel cell column "C9". I just want to get last page number in excel vba please help me


  • To post as a guest, your comment is unpublished.
    sroczeto@gmail.com · 1 years ago
    Hello, works great, thank you for sharing this. One question, is it possible to add that also counts microsoft word .doc and .docx files?
    • To post as a guest, your comment is unpublished.
      skyyang · 11 months ago
      Hi, sroczeto,
      To count the page number of .doc and .docx as well as the PDF files, please apply the following 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
      Dim xWdApp
      Dim xWd
      Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
      If xFd.Show = -1 Then
      Application.ScreenUpdating = False
      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
      xFileName = Dir(xFdItem & "*.docx", vbDirectory)
      Set xWdApp = CreateObject("Word.Application")
      Do While xFileName <> ""
      Cells(I, 1) = xFileName
      xFileNum = FreeFile
      Set xWd = GetObject(xFdItem & xFileName)
      Cells(I, 2) = xWd.ActiveWindow.Panes(1).Pages.Count
      xWd.Close False
      I = I + 1
      xFileName = Dir
      Loop
      Columns("A:B").AutoFit
      End If
      Application.ScreenUpdating = True
      End Sub
      • To post as a guest, your comment is unpublished.
        sroczeto@gmail.com · 11 months ago
        Thanks mate! It works on pdf and docx, but not on doc files. And one question more, can yo uadd that this will count in subfolders too?
  • To post as a guest, your comment is unpublished.
    Asela · 1 years ago
    Thank you so much
  • To post as a guest, your comment is unpublished.
    Viviane · 1 years ago
    Awesome code! I cant get it to work in subfolders. Can anyone help me pleas?
  • To post as a guest, your comment is unpublished.
    JuleZz_St · 1 years ago
    Hello.

    Is there a way to also add the page number of the documents and also I get an error and this is the message:
    xStr = Space(LOF(xFileNum))


    Thank you very much.
  • To post as a guest, your comment is unpublished.
    Aleca Tesseris Sulli · 1 years ago
    oh i see, this is the whole code. I tried to add to the original and was getting an error. Thank you!
  • To post as a guest, your comment is unpublished.
    Daphne · 1 years ago
    wow. subfolders works great. can you share how to add "file path" and "file size" too?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Daphne,
      For solving your problem, please apply the below code, please try, hope it can help you!

      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
      Set xRg = Range("A1")
      Range("A:B").ClearContents
      Range("A1:B1").Font.Bold = True
      xRg = "File Name"
      xRg.Offset(0, 1) = "Pages"
      xRg.Offset(0, 2) = "Path"
      xRg.Offset(0, 3) = "Size(b)"
      I = 2
      Call SunTest(xFdItem, I)
      End If
      End Sub

      Sub SunTest(xFdItem As Variant, I As Long)
      Dim xRg As Range
      Dim xStr As String
      Dim xFd As FileDialog
      Dim xFileName As String
      Dim xFileNum As Long
      Dim RegExp As Object
      Dim xF As Object
      Dim xSF As Object
      Dim xFso As Object
      xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
      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
      Cells(I, 3) = xFdItem & xFileName
      Cells(I, 4) = FileLen(xFdItem & xFileName)
      I = I + 1
      xFileName = Dir
      Loop
      Columns("A:B").AutoFit
      Set xFso = CreateObject("Scripting.FileSystemObject")
      Set xF = xFso.GetFolder(xFdItem)
      For Each xSF In xF.SubFolders
      Call SunTest(xSF.Path & "\", I)
      Next
      End Sub
      • To post as a guest, your comment is unpublished.
        Daphne · 1 years ago
        This is so great. Thanks!
  • To post as a guest, your comment is unpublished.
    Mat · 1 years ago
    Wov! so many thanks for sharing, this VBA code is a killer!! It works flawlessly with Excel O365
  • To post as a guest, your comment is unpublished.
    Prashant Narayankar · 1 years ago
    What if I want to run through subfolders too?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Prashant,
      To get the number of all the PDF files from folder and subfolders, please apply the below 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
      Set xRg = Range("A1")
      Range("A:B").ClearContents
      Range("A1:B1").Font.Bold = True
      xRg = "File Name"
      xRg.Offset(0, 1) = "Pages"
      I = 2
      Call SunTest(xFdItem, I)
      End If
      End Sub

      Sub SunTest(xFdItem As Variant, I As Long)
      Dim xRg As Range
      Dim xStr As String
      Dim xFd As FileDialog
      Dim xFileName As String
      Dim xFileNum As Long
      Dim RegExp As Object
      Dim xF As Object
      Dim xSF As Object
      Dim xFso As Object
      xFileName = Dir(xFdItem & "*.pdf", vbDirectory)
      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
      Set xFso = CreateObject("Scripting.FileSystemObject")
      Set xF = xFso.GetFolder(xFdItem)
      For Each xSF In xF.SubFolders
      Call SunTest(xSF.Path & "\", I)
      Next
      End Sub

      Please try, hope it can help you!
      • To post as a guest, your comment is unpublished.
        ThomasB · 6 months ago
        Can you help me to also get the creator and dimensions of the file?
      • To post as a guest, your comment is unpublished.
        Aleca Tesseris Sulli · 1 years ago
        This is wonderful, thank you. I would like to run through subfolders too. Where/how in the above code do I add these additional commands? what would the whole thing look like?
      • To post as a guest, your comment is unpublished.
        Mat · 1 years ago
        Your subfolder code works fine! thanks
  • To post as a guest, your comment is unpublished.
    pedrohmc1@gmail.com · 1 years ago
    Regards

    There is a problem with the program, I am using version 2019 of Office, and the pages seem to be counting badly the first 9 accumulated pages I get zero, in the ninth accumulated page I get 10.

    Can you please help me with that inconvenience?

    Beforehand thank you very much.

    Atte.

    Pedro
    • To post as a guest, your comment is unpublished.
      Rob Haughey · 1 years ago
      The code is good structure for how to do this kind of thing but that regexp will give unreliable results for many pdfs. The regexp being searched for (/Type\s*/Page[^s]), will not work in SECURED pdfs (count will be zero). Also pdfs tools and versions vary in how they mark pages. It could be accurate if you know that all your pdfs are created using the same structure (version and tools).
      • To post as a guest, your comment is unpublished.
        Pedro Marza · 6 months ago
        Thank you very much for your answer, I solved the problem by saving the files as: "Optimized PDF"
        • To post as a guest, your comment is unpublished.
          Dave · 2 months ago
          100% agree with Pedro, I was having the same problem as Rob where some PDF page counts were wrong. But if you make sure that all files are saved as "Optimized PDF" in the folder it will get all the pages correct. This worked for me on over 100 separate PDF files. You can bulk optimize as well with Acrobat Pro. Overall great code, worked right out of the box if you will.
  • To post as a guest, your comment is unpublished.
    Suzie · 1 years ago
    HOLY! This is awesome! Thank you so much! I'm a printer and have been doing printit.txt and filling in by hand! This is going to make quoting and checking jobs SO MUCH EASIER! Thanks again!!!
  • To post as a guest, your comment is unpublished.
    Pedro · 1 years ago
    Saludos


    Hay algún problema con el programa, yo estoy usando la versión 2019 de Office, y las páginas parece que las va contando de mal las primeras 9 páginas acumuladas me sale cero, en la novena página acumulada me sale 10.

    ¿Por favor me puedes ayudar con ese inconveniente?

    De antemano muchas gracias.

    Atte.

    Pedro
  • To post as a guest, your comment is unpublished.
    Fawaz · 1 years ago
    Not working properly, for some pdfs, for some pdfs it shows 0 and for some incorrect page numbers
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi, Fawaz,
      The code works well in my Excel, which Excel version do you use?
      Or you can send your detailed problem or pdf files to my Email: skyyang@extendoffice.com.
      • To post as a guest, your comment is unpublished.
        JC · 1 years ago
        Hi skyyang,

        I've the same problem as Fawaz. I use MS Office Professional Plus 2013.

        Thanks for your help!

        Best regards
  • To post as a guest, your comment is unpublished.
    Chase C · 2 years ago
    Works great! Many thanks!
    • To post as a guest, your comment is unpublished.
      Merlin · 10 months ago
      Thank you very much for posting such informative message