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

複数のWord文書をPDFファイルにバッチ変換する方法は?

Wordの[名前を付けて保存]機能を使用すると、Word文書をPDFファイルに簡単に変換できます。 しかし、フォルダ内のすべてのWord文書を一度に別々のPDFファイルに変換したい場合、どうすればそれを実現できますか? この記事では、複数のWord文書をWordでPDFファイルにバッチ変換するVBAコードを紹介します。

複数のWord文書をVBAでPDFファイルにバッチ変換する
Kutools for Wordを使用して、複数のWordドキュメントをPDFファイルにバッチ変換します


複数のWord文書をVBAでPDFファイルにバッチ変換する

次のVBAコードは、フォルダ内のすべてのWord文書を一度にPDFファイルにすばやく変換するのに役立ちます。 次のようにしてください。

1. Wordで、を押します 他の + F11 キーを押して アプリケーション向け Microsoft Visual Basic 窓。

2。 の中に アプリケーション向け Microsoft Visual Basic ウィンドウ、クリック インセット > モジュール、次に以下のVBAコードをモジュールウィンドウにコピーします。

VBAコード:複数のWord文書をWordのPDFファイルにバッチ変換します

Sub ConvertWordsToPdfs()
'Updated by Extendoffice 20181123
    Dim xIndex As String
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim xNewName As String
    Dim xFileName As String
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    xFolder = xDlg.SelectedItems(1) + "\"
    xFileName = Dir(xFolder & "*.*", vbNormal)
    While xFileName <> ""
        If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then
            xIndex = InStr(xFileName, ".") + 1
            xNewName = Replace(xFileName, Mid(xFileName, xIndex), "pdf")
            Documents.Open FileName:=xFolder & xFileName, _
                ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
                PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
                WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
                wdOpenFormatAuto, XMLTransform:=""
            ActiveDocument.ExportAsFixedFormat OutputFileName:=xFolder & xNewName, _
                ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
                wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
                Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                BitmapMissingFonts:=True, UseISO19005_1:=False
            ActiveDocument.Close
        End If
        xFileName = Dir()
    Wend
End Sub

3。 プレス F5 コードを実行するためのキー。

4. [参照]ダイアログが表示されます。PDFファイルに変換するWordドキュメントが含まれているフォルダを選択し、[ OK

次に、選択したフォルダ内のすべてのWord文書が一度に個別のPDFファイルに変換されます。 スクリーンショットを参照してください:


Kutools for Wordを使用して、複数のWordドキュメントをPDFファイルにバッチ変換します

VBAコードの処理が難しい場合は、 ドキュメントを変換する の有用性 Kutools for Word この問題を解決します。

Kutools for Word :100以上の便利なWordアドインで、 制限なしで無料で試すことができます 60.

1。 クリック クツールズプラス > Doc / Docx。 スクリーンショットを参照してください:

2。 の中に ドキュメントフォーマットコンバーター ウィンドウでは、次のようにする必要があります。

2.1)PDFに変換するドキュメントが含まれているフォルダを選択します ソースファイルフォルダ セクション;
2.2選択 docxをpdfに変換する フォーマット変換 ドロップダウンリスト;
2.3)デフォルトでは、 ソースと同じ宛先パス ボックスがチェックされています。 変換されたすべてのPDFファイルを同じフォルダーに配置する場合は、このオプションを選択したままにします。
PDFファイルとソースドキュメントを分離する場合は、チェックを外す必要があります ソースと同じ宛先パス ボックスをクリックし、新しいフォルダを選択してPDFファイルを に保存 ボックス;
2.4)をクリックします 開始 ボタン。 スクリーンショットを参照してください:

3.次に、正常に変換されたドキュメントの数を示すダイアログボックスが表示されたら、[ OK ボタンを押して閉じます ドキュメントフォーマットコンバーター 窓。

これで、すべての.docx形式のドキュメントがpdfファイルに変換されます。

このユーティリティの無料トライアルをご希望の場合は、 ソフトウェアを無料でダウンロード まず、上記の手順に従って操作を適用します。

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

Kutools for Word - Over で単語体験を向上させましょう 100 注目すべき機能!

🤖 Kutools AI アシスタント: AI であなたの文章を変革します - コンテンツを生成する  /  テキストを書き換える  /  文書を要約する  /  情報のお問い合わせ 文書に基づいて、すべて Word 内で

📘 ドキュメントマスタリー: 分割ページ  /  ドキュメントをマージする  /  選択内容をさまざまな形式でエクスポート (PDF/TXT/DOC/HTML...)  /  PDFへの一括変換  /  ページを画像としてエクスポート  /  複数のファイルを一度に印刷する...

コンテンツ編集: バッチ検索と置換 複数のファイルにわたる  /  すべての画像のサイズを変更する  /  テーブルの行と列を転置する  /  表をテキストに変換...

🧹 楽にお掃除:振り払って 余分なスペース  /  セクションブレーク  /  すべてのヘッダー  /  テキストボックス  /  ハイパーリンク  / その​​他の取り外しツールについては、当社の Web サイトをご覧ください。 グループを削除...

クリエイティブインサート:挿入 千の区切り文字  /  チェックボックス  /  ラジオボタン  /  QRコード  /  バーコード  /  斜線テーブル  /  方程式のキャプション  /  画像キャプション  /  テーブルキャプション  /  複数の写真  / 詳細については、 グループを挿入...

🔍 正確な選択: ピンポイント 特定のページ  /  テーブル  /  シェイプ  /  見出し段落  / ナビゲーションを強化する 他には? 機能の選択...

スターの強化: あらゆる場所に素早く移動  /  繰り返しのテキストを自動挿入  /  ドキュメントウィンドウをシームレスに切り替える  /  11 変換ツール...

👉 これらの機能を試してみませんか? Kutools for Wordは、 60日無料トライアル、制限なし! 🚀
 
Comments (36)
Rated 5 out of 5 · 3 ratings
This comment was minimized by the moderator on the site
Just found this, very helpful. Also thanks for the suggested mods above. I am struggling with the following:

If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then


It seems to ask to do the conversion if it's not a doc or docx? (Leaving aside that, I think, for docx it should be last 5 chars, not 4?) Anyway,
This comment was minimized by the moderator on the site
the code stopped. I clicked debug. code below was highlighed yellow

Please, any assistance? thank you

Documents.Open Filename:=xFolder & xFileName, _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
This comment was minimized by the moderator on the site
Thank you for this. It has just saved me so much time and provided education. I will peruse your site for more top tips.
Thank you again. Brilliant quick fix. One does have to ask, why do MS make such things so impossible to find in their products.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Здравствуйте! Огромное спасибо за модуль! Но есть проблема, у меня куча папок и в каждой папке больше двухсот файлов с разными именами.. Можно ли сделать так чтобы ваш модуль сохранял файлы в PDF не под номерами 1,2,3... и т.д, а под своими собственными именами файлов!? Помогите пожалуйста, и я буду молиться за вас ВЕЧНО!!))
This comment was minimized by the moderator on the site
Hi Егор,
I don't quite understand your question. After converting the file using the VBA code, the name of the PDF file is the same as the name of the Word file.
This comment was minimized by the moderator on the site
Здравствуйте! Большое спасибо вам, за то что вы есть! Спасибо за код он почти идеальный, но ОЧЕНЬ прошу Вас помочь с одним нюансом... Мне Очень важно чтоб ваш макрос сохранял в PDF не под номерами 1.2.3 и т.д, а брал название вордовского файла и сохранял под ним. У меня сто папок и в каждой по 251 файл со своим собственным названием... Мне это Очень нужно, пожалуйста помогите!?????.....
This comment was minimized by the moderator on the site
The VBA macro worked like a charm! :D
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi Myles,
Thank you for your feedback.
This comment was minimized by the moderator on the site
Hello is there a way to select a different output folder and not the original folder?

thank you
This comment was minimized by the moderator on the site
Hi Vincent,
Please try the following VBA code. After running the code, an Open window pops up, please select a folder containing the douments you want to convert, in the second popping up Save PDF files in window, select a folder to save the PDF files.

Sub ConvertWordsToPdfs()
'Updated by Extendoffice 20220805
  Dim xIndex As String
  Dim xDlg As FileDialog, xSaveDlg As FileDialog
  Dim xFolder As Variant, xSaveFolder As Variant
  Dim xNewName As String
  Dim xFileName As String
  On Error Resume Next
  Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
  xDlg.Title = "Open"
  xDlg.ButtonName = "Open"
  If xDlg.Show <> -1 Then Exit Sub
  xFolder = xDlg.SelectedItems(1) + "\"
  xFileName = Dir(xFolder & "*.*", vbNormal)
  Set xSaveDlg = Application.FileDialog(msoFileDialogFolderPicker)
  xSaveDlg.Title = "Save PDF files in"
  xSaveDlg.ButtonName = "Save"
  If xSaveDlg.Show <> -1 Then Exit Sub
  xSaveFolder = xSaveDlg.SelectedItems(1) + "\"
  Application.DisplayAlerts = wdAlertsNone
  While xFileName <> ""
      If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then
          xIndex = InStr(xFileName, ".") + 1
          xNewName = Replace(xFileName, Mid(xFileName, xIndex), "pdf")
          Documents.Open FileName:=xFolder & xFileName, _
              ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
              PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
              WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
              wdOpenFormatAuto, XMLTransform:=""
          ActiveDocument.ExportAsFixedFormat OutputFileName:=xSaveFolder & xNewName, _
              ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
              wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
              Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
              CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
              BitmapMissingFonts:=True, UseISO19005_1:=False
          ActiveDocument.Close wdSaveChanges
      End If
      xFileName = Dir()
  Wend
  Application.DisplayAlerts = wdAlertsAll
  Set xDlg = Nothing
  Set xSaveDlg = Nothing
End Sub
This comment was minimized by the moderator on the site
The code does not convert my Word files
This comment was minimized by the moderator on the site
This code is amazing and fast but if there is a link in the Doc file it will open a window stating there may be information linked to another file apply these changes? and then another asking to save as. Is there a way to add to the code to automatically do these steps for me? Out of ~800 there are ~40 and I must be present and do this every time.
thank you
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi Vincent,
The following VBA code can do you a favor. Please give it a try. Thank you for your feedback.

Sub ConvertWordsToPdfs()
'Updated by Extendoffice 20220805
  Dim xIndex As String
  Dim xDlg As FileDialog
  Dim xFolder As Variant
  Dim xNewName As String
  Dim xFileName As String
  On Error Resume Next
  Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
  If xDlg.Show <> -1 Then Exit Sub
  xFolder = xDlg.SelectedItems(1) + "\"
  xFileName = Dir(xFolder & "*.*", vbNormal)
  Application.DisplayAlerts = wdAlertsNone
  While xFileName <> ""
      If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then
          xIndex = InStr(xFileName, ".") + 1
          xNewName = Replace(xFileName, Mid(xFileName, xIndex), "pdf")
          Documents.Open FileName:=xFolder & xFileName, _
              ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
              PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
              WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
              wdOpenFormatAuto, XMLTransform:=""
          ActiveDocument.ExportAsFixedFormat OutputFileName:=xFolder & xNewName, _
              ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
              wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
              Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
              CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
              BitmapMissingFonts:=True, UseISO19005_1:=False
          ActiveDocument.Close wdSaveChanges
      End If
      xFileName = Dir()
  Wend
  Application.DisplayAlerts = wdAlertsAll
End Sub
This comment was minimized by the moderator on the site
Вот тут во втором условии надо 4 заменить на 5:
If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 5) <> ".docx") Then
Для исключения проблемы наличия точек можно сделать так:
xIndex = InStr(xFileName, ".doc") + 1
Ну и про советы других товарищей не забыть ;)

This comment was minimized by the moderator on the site
Thank you so much! Saved me so much time!
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