logo
search
PDF Conversion Issues

Export Excel Worksheet to PDF with a User-Selected File Name via VBA

Tauseeq MagsiTauseeq Magsi Oct 23, 2026 869 views

Question details

The user needs to write a VBA macro that prompts the user to select a save location and file name, and then exports the active Excel worksheet as a PDF.

How to Export an Excel Worksheet to PDF with a User-Selected File Name
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Creating a VBA macro to handle saving worksheets as PDFs while giving users control over the output file name and directory.
Observed behavior
Using Application.GetSaveAsFilename returns the desired file path as a string, but it does not automatically execute the PDF export or save the file.
Before you start

Ensure that your spreadsheet application supports macros and that you have enabled the Developer tab so you can access the Visual Basic for Applications (VBA) editor.

Solution 1Recommended

Combine GetSaveAsFilename with ExportAsFixedFormat

Use Application.GetSaveAsFilename to capture the desired file path from the user, and then pass that file path to the ExportAsFixedFormat method to execute the actual PDF creation.

The Application.GetSaveAsFilename method is designed only to open a file dialog and return the string path the user selects. It does not perform any saving action on its own.

To actually generate the PDF, you must explicitly call the ExportAsFixedFormat method on your worksheet, using the string returned by the prompt as your file name.

1
Open the VBA Editor

Press Alt + F11 in your spreadsheet to launch the Visual Basic for Applications (VBA) editor window.

2
Insert a New Module

In the top menu, click on 'Insert' and select 'Module' to create a blank workspace for your macro.

3
Add the Macro Code

Type the following VBA code into the module window: Sub SaveAsPDF() Dim vName As Variant vName = Application.GetSaveAsFilename(InitialFileName:="Test.pdf", FileFilter:="PDF files (*.pdf),*.pdf") If vName = False Then Exit Sub End If ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vName End Sub

4
Run the Macro

Press F5 or click the 'Run' button (the green play icon) on the toolbar. A dialog box will appear allowing you to specify the PDF name and save destination.

Combine GetSaveAsFilename with ExportAsFixedFormat
Handle User Cancellations: Always include an 'If vName = False Then Exit Sub' condition. This ensures that if the user clicks 'Cancel' in the save dialog, the macro stops safely instead of producing a runtime error.
Export to PDF Easily in WPS Office

Export Spreadsheets to PDF Without Writing Code

If you want a fast, hassle-free way to save your worksheets as PDFs with custom names, WPS Spreadsheet offers a built-in, intuitive PDF export feature that requires absolutely no VBA knowledge.

  1. 1. Open your workbook in WPS Spreadsheet: Launch WPS Office and open the spreadsheet file you wish to convert to PDF.
  2. 2. Access the Export to PDF tool: Navigate to the 'Tools' tab on the top ribbon and click on the 'Export to PDF' button.
  3. 3. Set your export options: In the popup window, choose whether you want to export the active sheet, the entire workbook, or a specific range.
  4. 4. Name and save your file: Click the 'Export to PDF' button, enter your desired file name in the system dialog box, and click 'Save'.
Built-in PDF converter requires no VBA programming or complex macrosFully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) formatsLightweight, fast, and features an intuitive interface for managing file exportsAllows you to customize export ranges, layouts, and file names in a single click
QA img-9

Frequently Asked Questions

Why isn't Application.GetSaveAsFilename actually saving my file?

The GetSaveAsFilename function only opens a standard dialog box to retrieve a file path and file name chosen by the user. It returns that information as a text string, but it is not designed to execute the file-saving command itself. You must pair it with a saving or exporting method, like ExportAsFixedFormat, to complete the process.

How do I set a default file name in the Save As dialog?

You can assign a default file name by using the InitialFileName parameter in your VBA code. For example, writing Application.GetSaveAsFilename(InitialFileName:="MonthlyReport.pdf") will pre-fill 'MonthlyReport.pdf' in the file name field when the dialog opens.

Can I restrict the save dialog so users can only choose PDF format?

Yes, you can restrict the file types shown in the dialog box by utilizing the FileFilter parameter. By passing FileFilter:="PDF files (*.pdf),*.pdf" to the GetSaveAsFilename function, the dialog will only display and accept files with a .pdf extension.

What happens if a user clicks Cancel on the save prompt?

When a user clicks 'Cancel' or closes the dialog box, the GetSaveAsFilename function returns a Boolean value of False rather than a text string. Attempting to save a file named 'False' will often cause an error, which is why your VBA macro must check if the variable equals False and exit the subroutine if it does.