VBA & Macro Problems

How to Save an Excel Workbook with Access VBA Using SaveAs

Muhammad TalhaMuhammad Talha Oct 29, 2026 870 views

Question details

The user needs to prompt a Save As dialog from Access VBA and successfully save an Excel macro-enabled workbook (.xlsm) with the correct file extension and format.

How to Save an Excel Workbook with Access VBA Using SaveAs
Product
Excel / Access VBA
Device & OS
not provided
Scenario
Automating Excel tasks from Microsoft Access via VBA and allowing the user to choose the save destination.
Observed behavior
The GetSaveAsFileName method only displays a Save As dialog and returns the selected path without actually saving the workbook or applying the correct file format.
Before you start

Ensure that your Microsoft Access database has the Microsoft Excel Object Library enabled in the VBA editor's References to allow smooth cross-application automation.

Solution 1Recommended

Use GetSaveAsFileName Followed by ActiveWorkbook.SaveAs

Prompt the user for a file location using GetSaveAsFileName, check if they canceled, and then execute the actual save command using the SaveAs method with the correct file format code.

The GetSaveAsFileName method simply brings up the dialog box and returns the file path string selected by the user. It does not perform any saving actions. To save the file in a specific format like a Macro-Enabled Workbook (.xlsm), you must use the ActiveWorkbook.SaveAs method and define FileFormat:=52.

1
Declare the File Name Variable

In your Access VBA editor, locate the subroutine controlling your Excel application object (e.g., XLapp) and declare a variable to store the path: `Dim strFileName As Variant`.

2
Invoke the Save As Dialog

Trigger the dialog box to get the desired file path using named arguments for clarity: `strFileName = XLapp.GetSaveAsFileName(InitialFileName:=path, FileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")`.

3
Check for User Cancellation

Add an If statement to verify the user did not click Cancel. Since GetSaveAsFileName returns a Boolean False on cancellation, use: `If strFileName <> False Then`.

4
Execute the Save Method

Inside the If block, invoke the actual save action using the path returned and specifying the macro-enabled format (52): `XLapp.ActiveWorkbook.SaveAs FileName:=strFileName, FileFormat:=52`.

Use GetSaveAsFileName Followed by ActiveWorkbook.SaveAs
Using Named Arguments: When using a named argument after the first argument in VBA, you must also use a named argument for the first one (e.g., InitialFileName:=path).
Free Microsoft Office alternative

Try WPS Office for Seamless Macro and Spreadsheet Management

While Microsoft Access and Excel offer specific VBA integrations, WPS Office provides a lightweight, highly compatible alternative for handling your complex spreadsheets. WPS Spreadsheet fully supports VBA macros, allowing you to run and edit your existing .xlsm files effortlessly.

  1. 1. Visit WPS Office: Go to the official WPS Office website on your browser.
  2. 2. Download the Installer: Click the Free Download button to get the latest installation package.
  3. 3. Install and Enable VBA: Run the installer, launch WPS Spreadsheet, and open your .xlsm files directly to continue your macro work.
Highly compatible with Microsoft Excel (.xlsx, .xlsm, .xls) file formats.Built-in VBA environment for creating, editing, and debugging spreadsheet macros.Free to use with a lightweight installation that saves system resources.Familiar user interface ensures zero learning curve for seamless migration.
microsoft office alternative - wps office

Frequently Asked Questions

What does FileFormat:=52 mean in Excel VBA?

In Excel VBA, FileFormat:=52 is the enumeration value for xlOpenXMLWorkbookMacroEnabled. It instructs the application to save the workbook specifically as an Excel Macro-Enabled Workbook with an .xlsm extension.

Why does GetSaveAsFileName create a file without an extension?

The GetSaveAsFileName method only returns a string representing the user's choice; it doesn't process file saving. If you omit the file type extension in your subsequent SaveAs command or don't explicitly declare the FileFormat, Excel will save the file based on default settings, often leading to missing or incorrect extensions.

How do I stop VBA from crashing if the user clicks Cancel in the Save As dialog?

When a user clicks Cancel, GetSaveAsFileName returns the Boolean value False instead of a string. To prevent crashes, capture the return value in a Variant variable and check `If YourVariable <> False Then` before running the SaveAs command.