提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
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 count pdf pages 1

5。 然后点击 OK 按钮,所有Pdf文件名和页码都列在当前工作表中,请参见截图:

doc count pdf pages 2


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
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.
    Mat · 3 days 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 · 20 days ago
    What if I want to run through subfolders too?
    • To post as a guest, your comment is unpublished.
      skyyang · 14 days 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.
        Mat · 3 days ago
        Your subfolder code works fine! thanks
  • To post as a guest, your comment is unpublished.
    Pedro Henrry Marza Colque · 3 months 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.
    Suzie · 3 months 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 · 3 months 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 · 5 months 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 · 4 months 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 · 4 months 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 · 6 months ago
    Works great! Many thanks!