Note: The other languages of the website are Google-translated. Back to English

How to batch convert multiple Word documents to pdf files?

It is easy for you to convert a Word document to a PDF file with the Save as function in Word. But if you want to convert all Word documents in a folder to separate PDF files at once, how can you achieve it? This article will show you a VBA code to batch convert multiple Word documents to pdf files in Word.

Batch convert multiple Word documents to pdf files with VBA
Batch convert multiple Word documents to pdf files with Kutools for Word


Batch convert multiple Word documents to pdf files with VBA

The following VBA code helps you quickly convert all Word documents in a folder to pdf files at once. Please do as follows.

1. In Word, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy below VBA code into the Module window.

VBA code: Batch convert multiple Word documents to pdf files in Word

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. Press the F5 key to run the code.

4. A Browse dialog pops up, please select the folder which contains Word documents you will convert to pdf files, and click the OK button.

Then all Word documents in selected folder are converted to separate pdf files at once. See screenshot:


Batch convert multiple Word documents to pdf files with Kutools for Word

If VBA code is hard to handle, you can try the Convert documents utility of Kutools for Word to solve the problem.

Kutools for Word : With more than 100 handy Word add-ins, free to try with no limitation in 60 days.

1. Click Kutools Plus > Doc/Docx. See screenshot:

2. In the Document Format Converter window, you need to do as follows:

2.1) Select the folder contains documents you will convert to pdf in the Source file folder section;
2.2 Select Convert docx to pdf from the Format Convert drop-down list;
2.3) By default, the Destination path same as source box is checked. If you want to place all converted pdf files in the same folder, just keep this option selected;
If you want to separate the pdf files and the source documents, you need to uncheck the Destination path same as source box, and select a new folder to save the pdf files in the Save to box;
2.4) Click the Start button. See screenshot:

3. Then a dialog box pops up to tell you how many documents have been successfully converted, click the OK button and close the Document Format Converter window.

Now all .docx format documents are converted to pdf files.

If you want to have a free trial of this utility, please go to free download the software first, and then go to apply the operation according above steps.


Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Comments (33)
Rated 5 out of 5 · 3 ratings
This comment was minimized by the moderator on the site
So nice blog, Thanks for sharing this blog. It Has so useful information for users. I liked this information so much.
Hope you keep sharing such kind of information convert word doc to html
This comment was minimized by the moderator on the site
VBA if i try to convert file that contains macros the macro swith to open file and ends without close file.
This comment was minimized by the moderator on the site
Is there any way to have the file name taken from the text of the document?
This comment was minimized by the moderator on the site
Hi there,


is there an easy way to adjust the VBA Code so that the Documents are convertet to .txt-Files?
This comment was minimized by the moderator on the site
Hi,

I get runtime error '424':
Object required

at the below code

Documents.Open Filename:=xFolder & xFileName, _

ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _

PasswordDocument:="", PasswordTemplate:="", Revert:=False, _

WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _

wdOpenFormatAuto, XMLTransform:=""


wdOpenFormatAuto = 0 it says.

Please help.

Thanks.
This comment was minimized by the moderator on the site
Hi,



I get runtime error '424':

Object required


image attached.




Please help.



Thanks.
This comment was minimized by the moderator on the site
Hello, Thank you so much for this code. It is working well, however, I get a dialog box after each conversion to save the word doc and I have to save. Can you include code in the macro to make that save and close the file instead of having to close each one?
This comment was minimized by the moderator on the site
if you add "ActiveDocument.Save" right above "ActiveDocument.Close" it will save the document before closing itself so you don't have to always hit the save prompt.
This comment was minimized by the moderator on the site
thanks for vba code , it helps a lot.
This comment was minimized by the moderator on the site
The VBA code is great. Cheers ,ate
This comment was minimized by the moderator on the site
Change xIndex = InStr(xFileName, ".") + 1


This comment was minimized by the moderator on the site
If you change
xIndex = InStr(xFileName, ".") + 1 to
xIndex = InStrRev(xFileName, ".") + 1
it will search from the end of the string to find the first period. This will avoid problems where the filename contains periods.
This comment was minimized by the moderator on the site
Спасибо, очень дельный совет
This comment was minimized by the moderator on the site
If you changexFileName = Dir(xFolder & "*.*", vbNormal)
to xFileName = Dir(xFolder & "*.doc*", vbNormal)the macro will not crash when there are non-Word files in the same folder.
This comment was minimized by the moderator on the site
This saved my day, thanks so much!
I would just like to point out for people using this, if the Word seems to freeze, just wait - it is generating the .pdf files. My Word froze but I opened the folder of the files, and could see that it was indeed generating the .pdf for all files.
This comment was minimized by the moderator on the site
VBA 很棒!!感謝您的分享。
This comment was minimized by the moderator on the site
I have a problem if documet has name 1.2.3.5.Blabla bla.doc it saves and rewrite PDFs and in conclusio I have only 0ne? last one document.How can I solve this problen?
This comment was minimized by the moderator on the site
If you change
xIndex = InStr(xFileName, ".") + 1
to
xIndex = InStrRev(xFileName, ".") + 1
it will search from the end of the string to find the first period. This will avoid problems where the filename contains periods.

If you change
xFileName = Dir(xFolder & "*.*", vbNormal)
to
xFileName = Dir(xFolder & "*.doc*", vbNormal)
the macro will not crash when there are non-Word files in the same folder.
This comment was minimized by the moderator on the site
Макрос работает некорректно, если в именах файлов есть точки
This comment was minimized by the moderator on the site
If you change
xIndex = InStr(xFileName, ".") + 1
to
xIndex = InStrRev(xFileName, ".") + 1
it will search from the end of the string to find the first period. This will avoid problems where the filename contains periods.
This comment was minimized by the moderator on the site
If you change
xIndex = InStr(xFileName, ".") + 1
to
xIndex = InStrRev(xFileName, ".") + 1
it will search from the end of the string to find the first period. This will avoid problems where the filename contains periods.

If you change
xFileName = Dir(xFolder & "*.*", vbNormal)
to
xFileName = Dir(xFolder & "*.doc*", vbNormal)
the macro will not crash when there are non-Word files in the same folder.
This comment was minimized by the moderator on the site
Thank you so much! Saved me so much time!
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
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
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
There are no comments posted here yet
Load More

Follow Us

Copyright © 2009 - www.extendoffice.com. | All rights reserved. Powered by ExtendOffice. | Sitemap
Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.
Protected by Sectigo SSL