ไปยังเนื้อหาหลัก

วิธีการนับจำนวนหน้าของไฟล์ Pdf ใน Excel

หากมีไฟล์ Pdf หลายไฟล์ในโฟลเดอร์เฉพาะตอนนี้คุณต้องการแสดงชื่อไฟล์ทั้งหมดเหล่านี้ในแผ่นงานและรับหมายเลขหน้าของแต่ละไฟล์ คุณจะจัดการกับงานนี้ใน Excel อย่างรวดเร็วและง่ายดายได้อย่างไร

นับจำนวนหน้าของไฟล์ Pdf จากโฟลเดอร์ในแผ่นงานด้วยรหัส VBA


นับจำนวนหน้าของไฟล์ Pdf จากโฟลเดอร์ในแผ่นงานด้วยรหัส VBA

อาจเป็นรหัส 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 AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
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