How to Use VBA to Convert Documents Batch to PDF
🔍 WiseChecker

How to Use VBA to Convert Documents Batch to PDF

Converting a large number of Word documents to PDF one by one is tedious and wastes time. You can automate this task with a VBA macro that processes every file in a folder at once. This article explains how to write and run a VBA script in Word that converts all .docx files in a source folder to PDF in a destination folder. By the end, you will have a reusable macro that saves hours of manual work.

Key Takeaways: Batch PDF Conversion with VBA in Word

  • Alt+F11 to open VBA editor: Access the Visual Basic for Applications environment to write your macro.
  • FileDialog(msoFileDialogFolderPicker): Lets the user select the source folder containing Word documents.
  • CreateObject(“Word.Application”): Opens a hidden instance of Word to process files without disturbing your current session.
  • Document.ExportAsFixedFormat with wdExportFormatPDF: Converts each document to PDF in the destination folder.

Understanding the VBA Batch Conversion Feature

VBA (Visual Basic for Applications) is a programming language built into Microsoft Office applications. With VBA, you can automate repetitive tasks such as converting multiple Word documents to PDF. The macro runs inside Word and can open, modify, and export documents without user interaction. Before writing the macro, ensure your Word installation includes VBA support, which is enabled by default in all desktop versions of Word. You do not need any third-party software. The macro uses the ExportAsFixedFormat method, which produces high-quality PDF files identical to the manual File > Save As > PDF process. The script loops through all .docx files in a chosen folder, opens each one, exports it as PDF, and closes it. You can adjust the destination folder inside the macro or let the script save PDFs in a subfolder named “PDF” inside the source folder.

Steps to Create and Run the Batch PDF Conversion Macro

Follow these steps to write, save, and execute the VBA macro. The macro prompts you to select a folder, then converts every .docx file in that folder to PDF. The PDF files are saved in a subfolder called “PDF” inside the source folder. If the subfolder does not exist, the macro creates it automatically.

  1. Open the VBA editor
    In Word, press Alt+F11 to open the Visual Basic for Applications editor. If you do not see the Project Explorer window, go to View > Project Explorer.
  2. Insert a new module
    In the Project Explorer, right-click on Normal or your document name and choose Insert > Module. A blank code window appears.
  3. Paste the macro code
    Copy and paste the following code into the module window:
    Sub BatchConvertToPDF()
        Dim fd As FileDialog
        Dim sourceFolder As String
        Dim fileName As String
        Dim doc As Document
        Dim pdfFolder As String
        Dim wordApp As Word.Application
    
        Set fd = Application.FileDialog(msoFileDialogFolderPicker)
        fd.Title = "Select folder containing Word documents"
        If fd.Show = -1 Then
            sourceFolder = fd.SelectedItems(1)
        Else
            MsgBox "No folder selected. Exiting macro.", vbExclamation, "Canceled"
            Exit Sub
        End If
    
        ' Ensure folder path ends with backslash
        If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    
        ' Create PDF output subfolder
        pdfFolder = sourceFolder & "PDF\"
        If Dir(pdfFolder, vbDirectory) = "" Then
            MkDir pdfFolder
        End If
    
        ' Get first .docx file
        fileName = Dir(sourceFolder & "docx")
    
        ' Create a hidden Word application instance
        Set wordApp = New Word.Application
        wordApp.Visible = False
        wordApp.DisplayAlerts = wdAlertsNone
    
        Do While fileName <> ""
            Set doc = wordApp.Documents.Open(sourceFolder & fileName)
            ' Convert to PDF
            doc.ExportAsFixedFormat _
                OutputFileName:=pdfFolder & Replace(fileName, ".docx", ".pdf"), _
                ExportFormat:=wdExportFormatPDF, _
                OpenAfterExport:=False, _
                OptimizeFor:=wdExportOptimizeForPrint, _
                Range:=wdExportAllDocument
            doc.Close SaveChanges:=False
            fileName = Dir()  ' Next file
        Loop
    
        wordApp.Quit
        Set wordApp = Nothing
    
        MsgBox "Conversion complete. PDF files saved in: " & vbCrLf & pdfFolder, vbInformation, "Done"
    End Sub
    
  4. Run the macro
    Press F5 while the cursor is inside the macro code, or go to the Word ribbon and click View > Macros, select BatchConvertToPDF, and click Run. A folder picker dialog appears. Navigate to the folder containing your .docx files and click OK.
  5. Wait for completion
    The macro processes each file silently. A message box appears when all conversions are finished. Open the “PDF” subfolder inside the source folder to see the generated PDF files.

Common Issues and How to Avoid Them

“Compile error: User-defined type not defined”

This error occurs if the VBA editor does not have a reference to the Word object library. In the VBA editor, go to Tools > References and ensure Microsoft Word 16.0 Object Library (or your version) is checked. Click OK and run the macro again.

Macro converts only a few files and stops

If Word encounters a corrupted document, the macro may stop. To handle errors, add the line On Error Resume Next right after Do While fileName <> "". This forces the macro to skip problematic files and continue with the rest. Add a counter to log skipped files if needed.

PDFs are saved in the wrong location

The macro creates a subfolder named “PDF” inside the source folder. If you prefer a different destination, change the line pdfFolder = sourceFolder & "PDF\" to a fixed path like pdfFolder = "C:\MyPDFs\". Ensure the folder exists or add a MkDir command for the custom path.

Macro does not appear in the Macros list

The macro must be saved in the Normal.dotm template or in the active document. If you pasted the code into a module inside the active document, the macro only runs when that document is open. To make the macro always available, paste the code into the Normal project (under Normal > Modules).

Manual Conversion vs VBA Macro: Key Differences

Item Manual Conversion VBA Macro
Time for 50 documents Approximately 90 minutes Less than 5 minutes
User interaction required Open each file, click Save As, select PDF, confirm Select folder once, macro handles the rest
Error handling Manual detection of corrupted files Can skip corrupted files automatically
Output folder control Must choose folder each time Fixed or dynamic subfolder
Skill level required Basic Word knowledge Basic VBA knowledge to create the macro

You can now convert hundreds of Word documents to PDF with a single click using the VBA macro. To extend the macro, add a second loop that processes .doc files by changing "docx" to "doc" and adding a separate Dir call. For advanced users, modify the ExportAsFixedFormat parameters to set PDF security options or reduce file size by using wdExportOptimizeForScreen instead of wdExportOptimizeForPrint.