メインコンテンツへスキップ

ExcelでPDFファイルのページ数を数える方法は?

特定のフォルダーに複数のPDFファイルがある場合は、これらすべてのファイル名をワークシートに表示し、各ファイルのページ番号を取得します。 Excelでこのジョブをすばやく簡単に処理するにはどうすればよいですか?

VBAコードを使用してワークシート内のフォルダーからPDFファイルのページ番号を数えます


VBAコードを使用してワークシート内のフォルダーからPDFファイルのページ番号を数えます

次のVBAコードは、すべてのPdfファイル名とその各ページ番号をワークシートに表示するのに役立ちます。次のようにしてください。

1。 PDFファイルとページ番号を取得するワークシートを開きます。

2。 を押し続けます Alt + F11 キー、そしてそれは開きます アプリケーション向け Microsoft Visual Basic 窓。

3に設定します。 OK をクリックします。 インセット > モジュール、次のマクロをに貼り付けます モジュール 窓。

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ファイルを含むフォルダーを選択し、ページ番号をカウントしてください。スクリーンショットを参照してください。

ドキュメント数pdfページ1

5。 次に、をクリックします OK ボタンをクリックすると、すべてのPDFファイル名とページ番号が現在のワークシートに一覧表示されます。スクリーンショットを参照してください。

ドキュメント数pdfページ2

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、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