031 大量のWordをPDF変換(VBA)

031 大量のWordをPDF変換のサンプルコードは下記です。
VBA画面で貼り付けてください^^
分からないこと等あったら、ご連絡よろしくお願いします!!
※素人の私が作っているので、もっと良い書き方たくさんあると思いますが、
 ご了承ください!

--------------------------------------------------------------------------------
Option Explicit

Dim strPath_1 As String, strPath_2 As String, buf As String, WordFilename As String, PdfFilename As String, saveFilename As String
Dim cnt As Long
Dim wdApp As Word.Application, wdDoc As Word.Document
Sub WordtoPDF()

Application.ScreenUpdating = False

strPath_1 = ActiveWorkbook.Path & "\Word\"
strPath_2 = ActiveWorkbook.Path & "\PDF\"
   
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True

buf = Dir(strPath_1 & "*doc*") 'ファイル名取得
   
If buf <> "" Then
    cnt = 0
    Do While buf <> ""
        WordFilename = buf
       
        Set wdDoc = wdApp.Documents.Open(strPath_1 & buf)
       
        PdfFilename = Left(WordFilename, InStr(WordFilename, ".") - 1) & ".PDF" 'Wordファイル名でPDF変換
        saveFilename = strPath_2 & PdfFilename
        wdDoc.ExportAsFixedFormat ExportFormat:=wdExportFormatPDF, outputFilename:=saveFilename 'PDF形式で保存
       
        wdDoc.Close SaveChanges:=wdDoNotSaveChanges

        buf = Dir()
        cnt = cnt + 1
    Loop
   
        Set wdDoc = Nothing
        wdApp.Quit
   
    Application.ScreenUpdating = True
   
    MsgBox cnt & "件、変換完了しました(^O^)"

Else
    MsgBox "Wordファイルがありません(泣)"
End If

Set wdApp = Nothing

End Sub

コメント