VBAでExcelファイルとWordファイルをPDF化するマクロ

仮想プリンタを使わなくてもPDF出力できます

PDF化と言うと仮想プリンタを使って印刷処理でPDF化する方法が有名ですが
現代のエクセルやワードはファイルの保存で普通にPDFファイルを保存することが可能です

昔ながらの仮想プリンタでPDF出力する画面
f:id:keiaruesu:20210422213445p:plain

現代では普通にPDF保存できるようになってる
Office2012あたりからできるようになった
f:id:keiaruesu:20210422214251p:plain

というわけでVBAでPDFファイル出力するときもExportAsFixedFormatというコマンドで簡単に出力可能になってます

エクセルファイルのPDF出力

エクセルのPDF保存の場合、いろいろ省略するとこんな感じになる

    'ワークブック
    Dim workbookObj As Workbook
    '開く
    Set workbookObj = Workbooks.Open(fileName:=excelFilePath, UpdateLinks:=0, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)

    'PDF出力
    Call workbookObj.ExportAsFixedFormat(xlTypePDF, pdfFilePath)

    'ブックを閉じる
    Call workbookObj.Close

 上記のような単純なWorkbooks.Openの場合、他人や自分が開いてるエクセルファイルを開こうとしていろいろ不都合が発生したりする。そのため下で紹介してるサンプルコードでは「Dim excelApp As New Excel.Application」を使ってプロセスを新しく作成してPDF化するファイルはそっちで開くようにしている。エクセル開く系の処理はこれを頻繁に使うと思うので覚えておいて損はない

ワードファイルのPDF出力

ワードのPDF保存の場合、いろいろ省略するとこんな感じになる

    '必要な参照設定
    'Microsoft Word 16.0 Object Library
    'wordの新規プロセス
    Dim wordApp As New Word.Application
    'wordドキュメントオブジェクト
    Dim wordDoc As Word.Document

    wordApp.Visible = True

    'ドキュメント開く
    Set wordDoc = wordApp.Documents.Open(wordFilePath, ReadOnly:=True)
    'PDF出力
    Call wordDoc.ExportAsFixedFormat(pdfFilePath, wdExportFormatPDF)

    'プロセス開放
    Call wordDoc.Close
    Call wordApp.Quit

 エクセルでワードファイル開くときは「Microsoft Word 16.0 Object Library」の参照設定が必要になるので注意しよう。サンプルコードでは参照設定なしでも動くコードもついでに書いておいたけど参照設定ある版のほうが色々楽だと思う
 エクセルでもワードでもPDF出力に使うのはExportAsFixedFormatという処理だけど渡す引数や引数の並びは全然違うので注意しよう、てかなんで共通じゃないんだこれ

サンプルコード

エクセルのPDF化とワードのPDF化
実行するときはエクセルにマクロ貼り付けてtestConvertPDFを実行してみてね

Option Explicit

'必要な参照設定
'Microsoft Word 16.0 Object Library

Public Sub testConvertPDF()

    Dim excelFilePath As String
    Dim pdfFilePath1 As String

    excelFilePath = "C:\サンプルエクセルファイル.xlsx"
    pdfFilePath1 = "C:\サンプルエクセルファイル.pdf"

    Call convertExcelToPdf(excelFilePath, pdfFilePath1)


    Dim wordFilePath As String
    Dim pdfFilePath2 As String
    
    wordFilePath = "C:\サンプルワードファイル.docx"
    pdfFilePath2 = "C:\サンプルワードファイル.pdf"
    
    Call convertWordToPdf(wordFilePath, pdfFilePath2)

End Sub


'エクセルファイルを開いてPDFファイルにして保存する処理
Private Function convertExcelToPdf(ByVal excelFilePath As String, ByVal pdfFilePath As String) As Boolean

    On Error GoTo errCatch

    'Excelの新規プロセス
    Dim excelApp As New Excel.Application
    'ワークブック
    Dim workbookObj As Workbook

    'プロセス独立で開く
    Set workbookObj = excelApp.Workbooks.Open(fileName:=excelFilePath, UpdateLinks:=0, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
'    'プロセス独立しないで開く
'    Set workbookObj = Workbooks.Open(fileName:=excelFilePath, UpdateLinks:=0, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
    
    'PDF出力
    Call workbookObj.ExportAsFixedFormat(xlTypePDF, pdfFilePath)

    'PDF化:成功
    convertExcelToPdf = True

    GoTo Finally
    Exit Function

errCatch:

    Debug.Print (Now & " : convertExcelToPdf : エラー ==========")
    Debug.Print ("Err.Source  : " & Err.Source)
    Debug.Print ("Err.Number : " & Err.Number)
    Debug.Print ("Err.Description : " & Err.Description)

    'PDF化:失敗
    convertExcelToPdf = False

    GoTo Finally
    Exit Function

Finally:

    'ブックを閉じる
    Call workbookObj.Close

    'Excelアプリケーションを閉じる
    Call excelApp.Application.Quit

End Function


'ワードファイルを開いてPDFファイルにして保存する処理
Public Function convertWordToPdf(ByVal wordFilePath As String, ByVal pdfFilePath As String) As Boolean
    
    On Error GoTo errCatch
    
    'wordの新規プロセス
    Dim wordApp As New Word.Application

    'wordドキュメントオブジェクト
    Dim wordDoc As Word.Document

    wordApp.Visible = True

    'ドキュメント開く
    Set wordDoc = wordApp.Documents.Open(wordFilePath, ReadOnly:=True)

    'PDF出力
    Call wordDoc.ExportAsFixedFormat(pdfFilePath, wdExportFormatPDF)
    
'    '参照設定「Microsoft Word 16.0 Object Library」を参照しないで実行する場合は以下のように書く
'
'    'wordの新規プロセス
'    Dim wordApp As Object
'    Set wordApp = CreateObject("Word.Application")
'
'    'wordドキュメントオブジェクト
'    Dim wordDoc As Object
'
'    wordApp.Visible = True
'
'    'ドキュメント開く
'    Set wordDoc = wordApp.Documents.Open(wordFilePath, ReadOnly:=True)
'
'    'PDF出力
'    Call wordDoc.ExportAsFixedFormat(pdfFilePath, 17)
    
    
    'PDF化:成功
    convertWordToPdf = True
    
    GoTo Finally
    Exit Function
    
errCatch:

    Debug.Print (Now & " : convertWordToPdf : エラー ==========")
    Debug.Print ("Err.Source  : " & Err.Source)
    Debug.Print ("Err.Number : " & Err.Number)
    Debug.Print ("Err.Description : " & Err.Description)
    
    'PDF化:失敗:
    convertWordToPdf = False
   
    GoTo Finally
    Exit Function
   
Finally:
    
    'プロセス開放
    Call wordDoc.Close
    Call wordApp.Quit
    
End Function