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应用程序 窗口。

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 · 6 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 · 6 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 · 8 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 · 9 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 · 8 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.
        Vidyadhar · 3 days ago
        Thanks this helps a lots.
  • To post as a guest, your comment is unpublished.
    ThomasB · 9 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 · 10 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 · 10 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 · 1 years 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 · 1 years 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 · 1 years 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 · 9 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 · 2 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 · 9 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 · 6 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 · 2 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 · 2 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 · 2 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 · 2 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 · 2 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 · 1 years ago
      Thank you very much for posting such informative message