A macro-based tool for automatically converting Word documents (.docx) to PDFs using VBA in Microsoft Word.
This macro allows you to quickly save your active Word document as a PDF with the same filename and location as the original .docx file. Follow the steps below to use this code.
- Microsoft Word (for VBA support).
- Basic knowledge of how to enable and run macros in Word.
-
Open your Word document: Make sure it's the document you want to save as a PDF.
-
Enable Developer Tab: If the Developer tab is not visible in Word, you can enable it by:
- Going to
File>Options. - In the Options window, select
Customize Ribbon. - Check the box for
Developeron the right side and clickOK.
- Going to
-
Create the Macro:
-
Open the Word document you want to convert to a PDF.
-
Go to the
Developertab and click onMacros. -
Enter a name for your macro (e.g.,
SaveAsPDF) -
Select
Normal.dotm (global template)from theMacros in:dropdown menu -
Now, click
Create. -
Copy and paste the following VBA code into the editor:
Sub SaveAsPDF() Dim docName As String Dim pdfPath As String ' Get the name of the active document (without the extension) docName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1) ' Set the PDF file path (same folder as the Word document) pdfPath = ActiveDocument.Path & "\" & docName & ".pdf" ' Save the document as PDF ActiveDocument.ExportAsFixedFormat OutputFileName:=pdfPath, _ ExportFormat:=wdExportFormatPDF MsgBox "PDF saved as: " & pdfPath End Sub
-
-
Run the Macro:
- After pasting the code, close the VBA editor.
- Go back to Word, and under the
Developertab, click onMacros. - Select
SaveAsPDFfrom the list and clickRun.
-
Result:
- The macro will automatically save the active Word document as a PDF in the same location with the same name (but with a
.pdfextension). - A confirmation message will pop up displaying the location of the saved PDF.
- The macro will automatically save the active Word document as a PDF in the same location with the same name (but with a
To make the macro run with the Ctrl+Alt+P keyboard shortcut, follow these steps:
-
Assign the Macro to Ctrl+Alt+P:
- Open Word and go to the
Filetab. - Click on
Optionsto open Word's settings. - In the
Word Optionswindow, selectCustomize Ribbon. - Click on the
Customize...button next toKeyboard shortcutsat the bottom of the window. - In the
Customize Keyboardwindow, under the "Categories" list, selectMacros. - In the "Macros" list, select the
SaveAsPDFmacro. - Click in the "Press new shortcut key" field and press Ctrl+Alt+P.
- If the shortcut is already assigned to something else, you can choose to overwrite it or pick another combination.
- Click
Assignand thenClose.
- Open Word and go to the
-
Use Ctrl+Alt+P:
- Now, you can simply press Ctrl+Alt+P anytime you want to save your active Word document as a PDF.
You can customize the file-saving location or add more export settings based on your needs. For example, you can modify the pdfPath to save the PDF in a different folder.