VBA & Macro Problems

How to Prompt for a PDF File Name in an Excel VBA Macro

Ayan MasoodAyan Masood Nov 3, 2026 870 views

Question details

The user wants to allow users to specify a destination and file name for a PDF export dynamically during the execution of an Excel VBA macro.

How to Prompt for a PDF File Name Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Exporting an active sheet to a PDF format using VBA while giving the user control over the save location and file name through a dialog box.
Observed behavior
The macro triggers a Save As dialog, captures the user's desired file path and name, and executes the PDF export to that specified location.
Before you start

Ensure you have enabled the Developer tab in your spreadsheet software and saved your workbook as a Macro-Enabled Workbook (.xlsm) before writing or running VBA code.

Solution 1Recommended

Use Application.GetSaveAsFilename to Capture PDF Save Location

Implement the GetSaveAsFilename method to display a Save As dialog, capture the user's chosen path, and pass it to the ExportAsFixedFormat function.

Instead of hardcoding a file path or forcing the macro to pause awkwardly, you can summon a standard Save As dialog box using VBA. This approach captures the file path the user chooses without actually saving a standard workbook file. Once the path is captured into a variable, you can use it to define the destination for your PDF export.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor. Insert a new Module from the Insert menu if you don't already have one.

2
Declare the Variable as Variant

Begin your macro by declaring your variable: Type 'Dim s As Variant'. A Variant is required because the prompt will return a Boolean 'False' if the user cancels the dialog.

3
Summon the Save As Dialog

Assign the dialog result to your variable: Type 's = Application.GetSaveAsFilename("*.pdf", "PDF Files (*.pdf),*.pdf")'. This ensures the dialog filters for PDF extensions.

4
Handle the Cancel Button

Add an IF statement to exit the macro if the user cancels: 'If s = False Then Exit Sub'.

5
Export the Active Sheet to PDF

Write the export command using the captured path: 'ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=s'.

Use Application.GetSaveAsFilename to Capture PDF Save Location
Complete VBA Snippet: Your complete code should look like this: Dim s As Variant s = Application.GetSaveAsFilename("*.pdf", "PDF Files (*.pdf),*.pdf") If s = False Then Exit Sub ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=s

Create and Run VBA Macros Easily in WPS Office

WPS Spreadsheet provides robust support for VBA and macros, allowing you to run your PDF export scripts seamlessly. You can easily prompt for file names and export sheets as PDFs without needing an expensive software subscription.

  1. 1. Download WPS Office: Download and install WPS Office Free from the official website.
  2. 2. Open your Workbook: Open your Macro-Enabled Workbook (.xlsm) in WPS Spreadsheet.
  3. 3. Access the Developer Tab: Navigate to the Developer tab to manage or write your VBA code.
  4. 4. Run your Macro: Click 'Macros' and run your script to trigger the Save As dialog and successfully export your sheet to PDF.
Fully compatible with Microsoft Excel VBA scripts and macro-enabled files (.xlsm).Built-in PDF converter allows seamless document export directly from spreadsheets.Free and lightweight Office suite with a familiar, easy-to-use interface.
microsoft office alternative - wps office

Frequently Asked Questions

What happens if the user clicks Cancel in the Save As dialog?

If the user clicks Cancel, the Application.GetSaveAsFilename method returns 'False'. Your VBA code must include an 'If' statement (e.g., If s = False Then Exit Sub) to check for this and safely exit the subroutine, otherwise, the macro will generate a runtime error.

Can I set a default file name in the Save As dialog box?

Yes, you can provide a default file name by inserting it before the extension wildcard in the InitialFilename parameter. For example: Application.GetSaveAsFilename("Invoice_123.pdf", "PDF Files (*.pdf), *.pdf").

Why am I getting a Type Mismatch error when running the macro?

This usually happens if you declare your file path variable as a 'String'. Because GetSaveAsFilename returns a Boolean (False) when canceled rather than a text string, the variable should be declared as a 'Variant' to handle both the string file path and the boolean cancel state.

Does this code work for exporting a specific range instead of the whole sheet?

Yes. To export a specific range, simply replace 'ActiveSheet' with your target range object in the export command. For example: Range("A1:D10").ExportAsFixedFormat Type:=xlTypePDF, Filename:=s.