Add a Password to an Excel Workbook from Access VBA
Question details
Add an opening password to an Excel workbook exported from Microsoft Access using VBA.

- Product
- Microsoft Access and Microsoft Excel
- Device & OS
- not provided
- Scenario
- An Access VBA procedure exports a workbook and needs to protect it without maintaining a separate Excel macro.
- Observed behavior
- TransferSpreadsheet creates the workbook, but the password must be applied afterward through Excel automation.
Ensure desktop Microsoft Access and Excel are available for automation, and identify the Access table or query to export. Choose writable paths for the temporary and protected workbooks; the example requires both paths to be unused.
Export from Access and apply an Excel opening password
Use this approach to keep the export and password-protection steps together in Access VBA.
TransferSpreadsheet does not directly set an Excel opening password. After the export, Excel automation opens the temporary workbook and saves a protected copy using the SaveAs Password argument.
The example uses late binding, so it does not require adding an Excel object library reference. It uses file format 51 for an .xlsx workbook.
In Access, press Alt+F11, then choose Insert > Module. Replace ExportQuery and both example paths in the following procedure with your own query or table name and writable .xlsx paths.
Paste this code into the module: Public Sub ExportProtectedWorkbook(ByVal openPassword As String) Dim xlApp As Object Dim wb As Object Dim tempPath As String Dim finalPath As String Dim errorMessage As String On Error GoTo Failed tempPath = "C:\Exports\temporary-export.xlsx" finalPath = "C:\Exports\protected-export.xlsx" If Len(openPassword) = 0 Then Err.Raise vbObjectError + 1, , "An opening password is required." If Len(Dir$(tempPath)) > 0 Or Len(Dir$(finalPath)) > 0 Then Err.Raise vbObjectError + 2, , "Choose paths that do not already exist." End If DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "ExportQuery", tempPath, True Set xlApp = CreateObject("Excel.Application") Set wb = xlApp.Workbooks.Open(tempPath) wb.SaveAs finalPath, 51, openPassword wb.Close False Set wb = Nothing xlApp.Quit Set xlApp = Nothing Kill tempPath MsgBox "Protected workbook saved: " & finalPath, vbInformation Exit Sub Failed: errorMessage = Err.Description On Error Resume Next If Not wb Is Nothing Then wb.Close False If Not xlApp Is Nothing Then xlApp.Quit Set wb = Nothing Set xlApp = Nothing MsgBox "Export failed: " & errorMessage & vbCrLf & _ "Check the temporary and output paths for any files created by this attempt.", vbExclamation End Sub
Call ExportProtectedWorkbook from your Access code, passing a nonempty password obtained at runtime. Open the protected output in Excel and confirm that it requests the password.

Open and edit exported Excel workbooks with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with Office files. Its format compatibility can make it easier to move everyday spreadsheet work from Microsoft Office; it does not replace the Access-to-Excel automation in this solution.

Frequently Asked Questions
Can TransferSpreadsheet set the Excel password directly?
No. Export the workbook with DoCmd.TransferSpreadsheet, then use Excel automation to open it and call Workbook.SaveAs with the Password argument.
Do I need a macro inside the exported Excel workbook?
No. The Access VBA procedure can create the Excel application object, open the exported workbook, save a password-protected copy, and close Excel.
Does this password prevent users from editing a worksheet?
The SaveAs Password argument sets a password required to open the workbook. Worksheet protection is a separate Excel feature configured through Review > Protect Sheet.
Why does the example use a temporary workbook?
Access first creates an unprotected export, which Excel then saves as a separate protected file. The example deletes the temporary file after success; if an error occurs, check the temporary path and remove any unprotected file securely.
How can I avoid storing the password in VBA code?
Obtain the password at runtime and pass it to the procedure rather than placing it in the module. Restrict access to the application and VBA project as an additional precaution.




