VBA & Macro Problems

How to Create and Open an Excel PDF via VBA Without Saving an Unwanted Copy

Kushani NimanthikaKushani Nimanthika Oct 26, 2026 868 views

Question details

The user needs to export an Excel workbook or worksheet to a PDF using a VBA macro and immediately view it, without prompting the user to save a local copy.

How to Create and Open an Excel PDF via VBA Without Saving an Unwanted Copy
Product
Excel
Device & OS
not provided
Scenario
Generating a PDF from an Excel worksheet via VBA macros for immediate viewing.
Observed behavior
The default macro behavior typically prompts for a save destination and retains the file. The goal is to bypass the save prompt, generate the PDF, and open it directly without keeping an unwanted file.
Before you start

Ensure that the Developer tab is enabled in your spreadsheet settings so you can access the VBA Editor, and check that macros are enabled for your workbook.

Solution 1Recommended

Use ExportAsFixedFormat with OpenAfterPublish Set to True

By directly calling the ExportAsFixedFormat method and omitting the GetSaveAsFilename prompt, you can generate and open the PDF instantly.

To prevent Excel from prompting the user for a save destination, you must avoid using Application.GetSaveAsFilename. Instead, specify the ExportAsFixedFormat parameters directly within the active sheet and set OpenAfterPublish:=True.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module or Object

Double-click the worksheet or module where you want to add the code, or insert a new Command Button and double-click it to access its Click event.

3
Enter the VBA Code

Paste the following code into the editor: Private Sub CommandButton1_Click() Application.ScreenUpdating = False ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, OpenAfterPublish:=True Application.ScreenUpdating = True End Sub

4
Run the Macro

Close the VBA editor and click your Command Button (or run the macro from the Developer tab). The PDF will generate and open automatically without asking for a file name.

Use ExportAsFixedFormat with OpenAfterPublish Set to True
Automatic File Handling: This method may still save a temporary PDF to a default location in the background, but it successfully bypasses the unwanted user prompt.
Efficient Excel & VBA Alternative

Use WPS Spreadsheet for VBA Macros and Fast PDF Export

WPS Office offers robust VBA support and built-in PDF conversion tools, allowing you to run your macros smoothly while maintaining full compatibility with Microsoft Excel formats.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet.
  2. 2. Enable the Developer Tab: Navigate to the ribbon, click on the Developer tab, and select 'Macros' or 'VBA Editor'.
  3. 3. Run Your Export Script: Paste your ExportAsFixedFormat script into the editor and run it directly to generate and view your PDF.
Fully supports Microsoft Excel .xlsm and .xlsb macro-enabled formats.Built-in VBA editor to write, debug, and execute macros seamlessly.Native one-click PDF export functionality.Lightweight, fast, and completely free to download.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA code keep prompting me to save the PDF?

If your VBA script includes 'Application.GetSaveAsFilename', it will trigger a prompt asking the user to choose a save destination. Removing this line and executing 'ExportAsFixedFormat' directly will bypass the prompt.

Can I use VBA to delete the PDF immediately after opening it?

Attempting to delete the file immediately via VBA using the 'Kill' command often fails because the PDF viewer locks the file while it is open. It is recommended to save the file to a temporary folder instead, which Windows will eventually clean up.

Does WPS Office support VBA commands like ExportAsFixedFormat?

Yes, WPS Spreadsheet has excellent VBA compatibility. You can run standard Excel VBA scripts, including 'ExportAsFixedFormat', to generate PDFs seamlessly within WPS Office.