Others

Detect and Close an Open Excel Workbook from Access VBA

Huda QurayshiHuda Qurayshi Nov 25, 2026 869 views

Question details

Determine whether a specific Excel workbook is open and close it without saving before an Access spreadsheet transfer.

Detect and Close an Open Excel Workbook from Access VBA
Product
Microsoft Access and Microsoft Excel
Device & OS
not provided
Scenario
An Access procedure exports a table to a date-named workbook that users may already have open when they run the process again.
Observed behavior
TransferSpreadsheet can fail when the target workbook is open and its file is in use.
Before you start

Identify the workbook's full file path and the Access table or query you want to export. Closing the workbook without saving discards any unsaved changes, so notify users before running the procedure.

Solution 1Recommended

Find and close the workbook through Excel automation

Use this approach to inspect an existing Excel instance before exporting from Access.

The example uses late binding, so it does not require an Excel object-library reference. It attaches to an existing Excel instance rather than opening the workbook to test its status.

1
Open the VBA editor

In Access, press Alt+F11, then select Insert > Module.

2
Add the export procedure

Paste the following code into the module and replace the workbook path and table name with your own values: Public Sub ExportAfterClosingWorkbook() Dim xlApp As Object Dim wb As Object Dim workbookPath As String Dim i As Long workbookPath = "C:\Exports\DailyReport.xlsx" On Error Resume Next Set xlApp = GetObject(, "Excel.Application") On Error GoTo 0 If Not xlApp Is Nothing Then For i = xlApp.Workbooks.Count To 1 Step -1 Set wb = xlApp.Workbooks(i) If StrComp(wb.FullName, workbookPath, vbTextCompare) = 0 Then wb.Close SaveChanges:=False End If Set wb = Nothing Next i End If Set xlApp = Nothing DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _ "TableName", workbookPath, True End Sub

3
Run the procedure

Place the cursor inside ExportAfterClosingWorkbook and press F5, or call the procedure from your Access workflow. The transfer runs after any matching workbook in the attached Excel instance is closed.

Find and close the workbook through Excel automation
Excel instance and file-lock limits: GetObject may attach to only one running Excel instance. A workbook in another instance, on another user's computer, or locked by another process may still block the transfer. The code does not quit Excel because it attached to a user's existing session.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight, easy-to-use alternative for working with spreadsheets. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files, although it does not fix Access VBA or TransferSpreadsheet errors.

Open and edit spreadsheets with WPS SpreadsheetsCompatible with Microsoft Excel XLS and XLSX formatsUse a familiar interface to move between Office filesFree and lightweight for everyday document work
microsoft office alternative - wps office

Frequently Asked Questions

Why shouldn't I open the workbook before checking it?

Opening it through Workbooks.Open makes it appear in Excel's Workbooks collection, so the check cannot tell whether it was already open. Use GetObject to inspect an existing Excel instance first.

How do I close the workbook without saving changes?

After finding the matching workbook in Excel's Workbooks collection, call wb.Close SaveChanges:=False. Any unsaved changes in that workbook are discarded.

Will checking whether the file exists tell me if it is open?

No. A file can exist whether it is open or closed. Check Excel's Workbooks collection for the full path, while accounting for other Excel instances and file locks.

Should the Access procedure quit Excel after closing the workbook?

Not when it attaches to an existing user session with GetObject: quitting Excel could affect other open workbooks. Release the object reference instead. If your code creates a separate Excel instance, quit that instance when its work is finished.