Detect and Close an Open Excel Workbook from Access VBA
Question details
Determine whether a specific Excel workbook is open and close it without saving before an Access spreadsheet transfer.

- 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.
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.
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.
In Access, press Alt+F11, then select Insert > Module.
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
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.

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.

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.




