How to Fix Excel VBA Runtime Error 1004 When Exporting PDF
Question details
The user is encountering Runtime Error 1004 in Excel VBA when attempting to export a worksheet or a specific range to a PDF file.
- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running a VBA macro to export a range (e.g., A1:L43) or an entire worksheet as a PDF file using the ExportAsFixedFormat method.
- Observed behavior
- The macro halts and displays 'Runtime error 1004', typically because the destination folder path is invalid, missing, or the user lacks proper write permissions.
Before troubleshooting, ensure that you have administrative or write privileges for the directory where you intend to save the exported PDF file.
Verify and Validate the Destination Folder Path
Ensure the target folder actually exists and the path string in your VBA code is formatted correctly before calling the ExportAsFixedFormat method.
The most common cause of Error 1004 during a PDF export is providing a file path that points to a folder that does not exist on the current computer.
Add a 'Debug.Print' or 'MsgBox' statement right before the export command in your VBA editor to display the exact file path your code is generating.
Manually navigate to the generated path in File Explorer to confirm the folder structure actually exists.
Use the 'Dir' function in your VBA script to check if the directory exists. If it does not, use the 'MkDir' command to create the folder dynamically before exporting.
Check Write Permissions and Open Files
Confirm that your user account has permission to save files in the specified location and that the target PDF is not currently open.
Use WPS Spreadsheets for Reliable PDF Exports and VBA Support
WPS Office offers robust VBA support and a highly stable built-in PDF converter, minimizing runtime errors when executing complex macros or saving specific data ranges as PDFs.
- 1. Open your Workbook in WPS: Launch WPS Spreadsheets and open your existing Excel (.xlsm or .xlsb) file containing the macro.
- 2. Enable Macros: Click on 'Enable Macros' in the security warning prompt at the top of the workspace.
- 3. Run the Export Macro: Navigate to the Developer tab, click 'Macros', select your PDF export macro, and click 'Run'.
- 4. Use Native PDF Export (Optional): Alternatively, highlight your range, go to the 'Menu' button, and select 'Export to PDF' to easily bypass VBA errors entirely.

Frequently Asked Questions
What does Runtime Error 1004 mean in Excel VBA?
Runtime Error 1004 is a generic error code in Excel VBA that typically indicates an application-defined or object-defined error. In the context of exporting PDFs, it usually means the file path is invalid, the destination folder is missing, or write access is denied.
How do I export a specific range to PDF using VBA?
You can use the ExportAsFixedFormat method directly on a Range object. For example: Range("A1:L43").ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\path\file.pdf".
Can I ignore Error 1004 using 'On Error Resume Next'?
While 'On Error Resume Next' will bypass the error prompt in VBA, it will not execute the PDF export if the path is invalid. It is better to handle the error by validating the folder path before calling the export method.
Why does my macro work on one computer but return Error 1004 on another?
This usually happens because the destination file path is hardcoded to a specific user directory (e.g., C:\Users\SpecificUser\Desktop). You should use environment variables to make the path dynamic and accessible for any user.




