Home>Blogs>VBA>Word to PDF Converter in Excel VBA: Convert Multiple Files
Word to PDF Converter in Excel VBA: Convert Multiple Files
VBA Templates

Word to PDF Converter in Excel VBA: Convert Multiple Files

A Word to PDF converter built in Excel VBA turns a whole folder of Word documents into PDF files with one click. You type two folder paths on a sheet, press a button, and the macro opens every Word file in the background, saves it as a PDF in the output folder and closes it again, while the status bar counts the progress. In this tutorial you build that converter step by step with the File System Object and the Microsoft Word object library, exactly as shown in the video, and then make it a little more robust.

Word to PDF converter in Excel VBA with Word folder path, PDF folder path and a Convert Word to PDF button
The finished converter: two folder paths and one button that converts every Word file in the folder to PDF.

Watch the Video Tutorial

The video first converts eight Word files into eight PDFs with one click, then writes the whole macro from a blank workbook, fixes two first-run errors and adds a progress counter in the status bar.

What You Will Build

  • An input sheet with a Word folder path and a PDF folder path.
  • A macro that loops through every file in the Word folder with the File System Object.
  • Background Word automation: each document is opened, exported as PDF with the same file name and closed without saving.
  • A progress message such as Processing …3/8 in Excel’s status bar and a Process Completed message at the end.
  • A Convert Word to PDF button that runs the macro.

If you have already followed the Excel to PDF converter in VBA tutorial, this one will feel familiar: the folder loop is the same, only the application that does the export changes.

Part 1 – Set Up the Converter Sheet

Step 1: Create the two folder paths

Press Ctrl+N for a new workbook. On Sheet1 type the labels and paste the folder paths:

  • D2: Word Folder Path, E2: the folder that holds your Word files, for example C:\Users\YourName\Desktop\Word Files.
  • D3: PDF Folder Path, E3: the folder where the PDF files should be saved, for example C:\Users\YourName\Desktop\PDF Files.

Both folders must already exist. The macro reads the paths from E2 and E3, so you can point it at a different folder any time without touching the code. (In PK’s finished file the same two paths sit lower on the sheet, in E13 and E14, under a Word-to-PDF graphic. If you use that layout, change the two cell addresses in the code.)

Step 2: Add the two references

Open the Visual Basic Editor (Developer > Visual Basic or Alt+F11), choose Insert > Module, then go to Tools > References and tick:

  • Microsoft Scripting Runtime – gives you the File System Object, Folder and File types used to loop through the folder.
  • Microsoft Word 15.0 Object Library – lets Excel create and control Word. 15.0 is Office 2013; Office 2016, 2019, 2021 and Microsoft 365 show 16.0. Pick whatever number your PC lists.
VBA References dialog with Microsoft Scripting Runtime and Microsoft Word 15.0 Object Library ticked
Tick both references. If they are not near the top of the list, scroll down to find them.

Part 2 – Write the Word to PDF Converter Macro

Step 3: Declare the objects

Start a subroutine called Convert_WordtoPDF and declare the sheet, the File System Object and the Word objects:

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")

Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File

Dim wordapp As New Word.Application
Dim worddoc As Word.Document
  • sh points to Sheet1, where the two paths live.
  • fso is the File System Object; fo will hold the Word folder and f each file inside it.
  • wordapp is a new, invisible copy of Word, and worddoc is the document currently being converted.

Note the word New in Dim wordapp As New Word.Application. In the video the first test run failed because it was missing: without New (or a Set ... = New line) there is no Word application to open files with.

Step 4: Loop through the Word folder and export each file

Set fo = fso.GetFolder(sh.Range("E2").Value)

For Each f In fo.Files
    Set worddoc = wordapp.Documents.Open(f.Path)
    worddoc.ExportAsFixedFormat sh.Range("E3").Value & Application.PathSeparator & VBA.Replace(f.Name, ".docx", ".pdf"), wdExportFormatPDF
    worddoc.Close False
Next

wordapp.Quit
MsgBox "Process Completed"

How the code works

  • fso.GetFolder(sh.Range("E2").Value) returns the Word folder as a Folder object, and For Each f In fo.Files visits every file in it.
  • f.Path is the complete path including the file name and extension, which is exactly what Documents.Open needs.
  • ExportAsFixedFormat takes two arguments here: the output file name and the format. The output name is the PDF folder from E3, a path separator (Application.PathSeparator returns the backslash), and the original name with .docx replaced by .pdf. So Test-2.docx becomes Test-2.pdf.
  • wdExportFormatPDF tells Word to write a PDF. Leaving this argument out was the second error in the first test run.
  • worddoc.Close False closes the document without saving, so your Word files are never changed.
  • wordapp.Quit closes the hidden Word application after the last file, then a message box confirms the job is done.

Step 5: Show the progress in the status bar

With hundreds of files you want to see that something is happening. Add these lines:

Application.DisplayStatusBar = True
Application.ScreenUpdating = False
...
Dim n As Integer

For Each f In fo.Files
    n = n + 1
    Application.StatusBar = "Processing ..." & n & "/" & fo.Files.Count
    ...
Next

Application.StatusBar = ""

n goes up by one for every file, and fo.Files.Count is the total number of files in the folder, so the status bar reads Processing …1/8, Processing …2/8 and so on. Setting Application.StatusBar back to an empty string at the end gives control of the status bar back to Excel.

The Complete VBA Code

Here is the full macro as it stands at the end of the video:

Option Explicit

Sub Convert_WordtoPDF()

Application.DisplayStatusBar = True
Application.ScreenUpdating = False

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")

Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File

Dim wordapp As New Word.Application
Dim worddoc As Word.Document

Set fo = fso.GetFolder(sh.Range("E2").Value)

Dim n As Integer

For Each f In fo.Files

    n = n + 1
    Application.StatusBar = "Processing ..." & n & "/" & fo.Files.Count

    Set worddoc = wordapp.Documents.Open(f.Path)
    worddoc.ExportAsFixedFormat sh.Range("E3").Value & Application.PathSeparator & VBA.Replace(f.Name, ".docx", ".pdf"), wdExportFormatPDF
    worddoc.Close False

Next

wordapp.Quit
Application.StatusBar = ""
MsgBox "Process Completed"

End Sub

Part 3 – Add the Button and Test It

Step 6: Assign the macro to a shape

Back in Excel, insert a rounded rectangle from Insert > Shapes, type Convert Word to PDF on it and centre the text. Right-click the shape, choose Assign Macro, set Macros in to This Workbook and pick Convert_WordtoPDF.

Step 7: Run the converter

Empty the PDF folder, click the button and watch the status bar count through the files. When Process Completed appears, open the PDF folder: you get one PDF per Word file, with the same name and the same content. Save the workbook as an .xlsm file so the macro is kept.

Improve the Loop (Optional Addition)

This section is an addition to the video, based on general VBA practice. The original loop sends every file in the folder to Word. If the folder also holds a .doc file, a PDF, an image or the hidden ~$ lock file Word creates while a document is open, the macro can stop or name the PDF wrongly. This version only converts .doc and .docx files and builds the PDF name from the file’s base name:

Dim ext As String
Dim pdfName As String

For Each f In fo.Files
    ext = LCase(fso.GetExtensionName(f.Name))
    If (ext = "docx" Or ext = "doc") And Left(f.Name, 2) <> "~$" Then
        n = n + 1
        Application.StatusBar = "Processing ..." & n
        pdfName = fso.BuildPath(sh.Range("E3").Value, fso.GetBaseName(f.Name) & ".pdf")
        Set worddoc = wordapp.Documents.Open(f.Path, ReadOnly:=True)
        worddoc.ExportAsFixedFormat pdfName, wdExportFormatPDF
        worddoc.Close False
    End If
Next
  • fso.GetExtensionName returns the extension without the dot, so the test is simple.
  • fso.GetBaseName returns the name without its extension, so both Report.doc and Report.docx become Report.pdf.
  • fso.BuildPath joins folder and file name and adds the backslash only if it is missing.
  • Opening with ReadOnly:=True avoids prompts for documents that are already open elsewhere.

Because some files are skipped, the counter shows only the number processed instead of n of total.

Common Errors and Fixes

Compile error: User-defined type not defined

A reference is missing. FileSystemObject, Folder and File need Microsoft Scripting Runtime; Word.Application and wdExportFormatPDF need the Microsoft Word Object Library. Tick both in Tools > References.

Run-time error 91: Object variable not set

wordapp was declared without New, so there is no Word application. Use Dim wordapp As New Word.Application as in the final code.

Run-time error 76: Path not found

The path in E2 is wrong or the folder does not exist. Copy the path from the File Explorer address bar and paste it into E2 and E3.

WINWORD.EXE keeps running after an error

If the macro stops halfway, wordapp.Quit never runs and a hidden Word stays open. Close it in Task Manager, fix the cause and run again. (Addition.)

Tips

  • Keep only Word documents in the input folder, or use the improved loop above.
  • Close the Word files before you run the converter, so Word does not show read-only or locked-file prompts.
  • To see the File System Object read file names, sizes and dates into a sheet, follow the File System Object tutorial to get files information in Excel.
  • Microsoft’s reference for Document.ExportAsFixedFormat lists the extra options, such as exporting a page range or optimising for print.

Want It Ready-Made?

If you would rather use a finished tool, the Automation: Excel to PDF Converter on NextGenTemplates.com converts a folder of Excel workbooks to PDF the same way, and the PDF to Word Converter Macro in Excel VBA goes the other direction. Browse all Excel utility and converter tools for more.

Related Tutorials

Frequently Asked Questions

Can Excel VBA convert Word files to PDF?

Yes. Excel VBA can start Microsoft Word in the background, open each document and call the ExportAsFixedFormat method of the Word document to save it as a PDF. You only need a reference to the Microsoft Word Object Library, or late binding with CreateObject.

Which references do I need for the Word to PDF converter?

Two: Microsoft Scripting Runtime for the File System Object that loops through the folder, and Microsoft Word 15.0 Object Library for the Word application and document objects. The version number changes with your Office version, for example 16.0 in Office 2016 and Microsoft 365.

Why does my macro give User-defined type not defined?

One of the two references is missing. Open Tools, References in the Visual Basic Editor and tick Microsoft Scripting Runtime and the Microsoft Word Object Library, then run the macro again.

Does the converter work with old .doc files?

The original macro only swaps .docx for .pdf in the file name, so a .doc file would be saved with the wrong name. The improved loop in this tutorial builds the PDF name from the base name of the file, which works for both .doc and .docx.

Will the macro change my original Word documents?

No. Each document is closed with SaveChanges set to False, so the Word files stay exactly as they were. Only new PDF files are written to the PDF folder.

Where can I get a ready-made converter?

NextGenTemplates.com sells finished converter tools such as the Automation Excel to PDF Converter and the PDF to Word Converter macro. See the Want It Ready-Made section above for the links.

About the Author

PK is a Microsoft Excel expert and trainer and the founder of PK: An Excel Expert and NextGenTemplates.com. He has been teaching Excel, VBA, Power Query and Power BI since 2016 on his YouTube channel. New to macros? Start with his free step-by-step VBA course.

Conclusion

A Word to PDF converter in Excel VBA needs just one loop: the File System Object walks through the Word folder, Word opens each document, ExportAsFixedFormat writes the PDF and the document closes without saving. Add the status bar counter and a button and you can turn hundreds of Word files into PDFs in a single click. Build it once, save it as an .xlsm and reuse it whenever a folder of documents needs converting.

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com

Leave a Reply