How to Automate Saving a Document as DOCX and PDF Using a Word VBA Macro
Question details
The user needs a Word VBA macro that can prompt for a file destination, save the document as a DOCX, export it as a PDF using the same base filename, and automatically open the PDF upon completion.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating the document saving process to simultaneously generate a DOCX file for editing and a PDF file for sharing, while opening the PDF automatically so it can be added to Recent Files or quickly attached in Outlook.
- Observed behavior
- The user wants to configure the FileDialog, SaveAs2, and ExportAsFixedFormat methods properly in VBA to achieve the simultaneous save and auto-open functionality.
Ensure you have the Developer tab enabled in your ribbon to access the VBA Editor, and verify that macro execution is allowed in your Trust Center settings.
Use ExportAsFixedFormat with OpenAfterExport Parameter
This is the most efficient method. It utilizes built-in VBA methods to save the DOCX file and simultaneously export and open the PDF without requiring additional shell commands.
By setting the OpenAfterExport parameter to True in the ExportAsFixedFormat method, the application will automatically launch the newly created PDF in your system's default PDF viewer as soon as the export finishes.
Press ALT + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.
In the top menu, click 'Insert' and select 'Module' to create a blank workspace for your macro code.
Use the 'Application.FileDialog(msoFileDialogFolderPicker)' method in your code to allow the user to select the folder where the files should be saved.
Once the path is selected, use the 'ActiveDocument.SaveAs2' method to save the file in standard DOCX format at the chosen destination.
Use the 'ActiveDocument.ExportAsFixedFormat' method with the same base filename. Crucially, include the argument 'OpenAfterExport:=True' to ensure the PDF opens automatically after it is saved.

Use Documents.Open to Open the PDF in the Word Processor
If you specifically need the PDF to open inside the word processing application itself (to add it to your Recent Files list for easy attachment in Outlook), you can explicitly command the application to open it.
Automate Document Tasks with WPS Office
WPS Writer offers robust support for VBA macros, allowing you to run complex automation scripts natively. You can easily adapt your document saving and exporting macros directly within WPS Office to streamline your workflow.
- 1. Download and Install WPS Office: Get the free WPS Office suite from the official website and install it on your computer.
- 2. Enable the Developer Tools: Open WPS Writer, navigate to the settings, and enable the Developer tab to access the VBA environment.
- 3. Write and Execute Your Macro: Click 'VBA Editor' on the Developer tab, paste your DOCX and PDF saving script, and run it to automate your document exports.

Frequently Asked Questions
Why isn't my PDF opening automatically after the macro finishes?
Ensure that you have included 'OpenAfterExport:=True' in your 'ExportAsFixedFormat' method. Additionally, verify that your computer has a default PDF viewer installed and properly associated with PDF files.
Can I automate the save process without the FileDialog prompt?
Yes. If you want to skip the destination prompt, you can hardcode a specific directory path (e.g., 'C:\Users\Username\Documents\MyFile.docx') directly into the 'SaveAs2' and 'ExportAsFixedFormat' functions.
How do I ensure the PDF gets added to Outlook's Recent Files?
Outlook pulls from the Windows Recent Items list. By simply opening the PDF file either via the 'OpenAfterExport:=True' trigger or the 'Documents.Open' method, the operating system typically registers the file in the Recent Items list, making it easily attachable in Outlook.
Does WPS Office support these specific VBA commands?
Yes, WPS Office Writer has excellent macro compatibility and supports standard VBA methods like 'SaveAs2' and 'ExportAsFixedFormat', allowing you to run similar automation scripts seamlessly.




