logo
search
VBA & Macro Problems

How to Fix Excel VBA Runtime Error 1004 When Exporting PDF

Maira MehtabMaira Mehtab Sep 22, 2026 868 views

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 you start

Before troubleshooting, ensure that you have administrative or write privileges for the directory where you intend to save the exported PDF file.

Solution 1Recommended

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.

1
Check the File Path Output

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.

2
Verify Folder Existence

Manually navigate to the generated path in File Explorer to confirm the folder structure actually exists.

3
Add Folder Creation Code

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.

Dynamic Paths for Shared Macros: If you are sharing the macro across different computers, avoid hardcoded paths like 'C:\Users\John\'. Use 'Environ("UserProfile")' to ensure the path is valid for any user executing the code.
Seamlessly Export PDFs with Macros

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. 1. Open your Workbook in WPS: Launch WPS Spreadsheets and open your existing Excel (.xlsm or .xlsb) file containing the macro.
  2. 2. Enable Macros: Click on 'Enable Macros' in the security warning prompt at the top of the workspace.
  3. 3. Run the Export Macro: Navigate to the Developer tab, click 'Macros', select your PDF export macro, and click 'Run'.
  4. 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.
Full compatibility with Microsoft Excel VBA macros (.xlsm) and scripts.Built-in, error-free PDF export functionality without needing third-party add-ins.Lightweight installation with a familiar, easy-to-use interface.Free to download and use for everyday office tasks.
microsoft office alternative - wps office

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.