プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

VBA PDFファイル印刷(簡易版)

■VBAで指定のフォルダ内のPDFファイルを一括印刷する方法(簡易版)

 
シンプルにする為にエラーチェック等の処理は入れていない
実際に業務等で使用したい場合は下記を参照
 
 
VBA フォルダ内のPDFファイル印刷
 
 

【事前準備】

「ツール」⇒「参照設定(R)」⇒「Windows Script Host Object Model」にチェックを入れておく
 


【ソースコード】


Sub PrintOutPdf()

    //WshShellのRunメソッドでプリントアウトする為の準備
    Dim obj As IWshRuntimeLibrary.WshShell
    Set obj = New IWshRuntimeLibrary.WshShell

    //PDFファイルを格納しているフォルダパスをセット
    Dim folderPath As String
    folderPath = "C:¥Users¥hogehoge¥Desktop¥TargetFolder"

    //フォルダ内のPDFファイル名の1つを取得し、セット
    Dim pdfName As String
    pdfName = Dir(folderPath & "¥" & "*.pdf")

    //プリンター名をセット
    //※プリンター名はイミディエイトウィンドウに「?Application.ActivePrinter」と記述すると取得可能(「on Ne00」の前までがプリンター名)
    Dim printerName As String
    printerName = "PX-047A Series(ネットワーク)"

    //プリントアウト用のコマンドをセットする変数
    Dim printOutCommand As String

    //フォルダ内のPDFファイルを全て取得するまでループ
    Do While pdfName <> ""
        ///PDFファイルを開く
        CreateObject("Shell.Application").ShellExecute (folderPath & "¥" & pdfName)

        //プリントアウト用のコマンドをセット
        printOutCommand = "AcroRd32.exe /t " & folderPath & "¥" & pdfName

        //プリントアウト用のコマンド実行
        obj.Run (printOutCommand)

       //次のPDFファイルを取得
        pdfName = Dir()
    Loop

End Sub