You can easily convert Word documents to PDF using the “Save As” function in Word. However, if you want to convert all Word documents in a folder to separate PDF files at once, how can you achieve that? This article will guide you with VBA code to batch convert multiple Word documents to PDF within Word.
Batch convert multiple Word documents to PDF using VBA:
The following VBA code helps you quickly convert all Word documents in a folder to PDF files at once. Here’s how:
- In Word, press Alt + F11 to open the Microsoft Visual Basic for Application window.
- In the Microsoft Visual Basic for Application window, click Insert > Module, then copy the VBA code below into the Module window.
‘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
- Press F5 to run the code.
- The Browse dialog pops up, please select the folder containing the Word documents you want to convert to PDF and click OK.
Afterward, all Word documents in the selected folder will be converted to separate PDF files at once. See the screenshot for reference.