How can I specify closing one of several open Excel files from Access? Access (triggered by an Excel macro) is turning around refresh data from an Excel front end. I already have the sheet saved, and I want Access to close it completely before opening the template and saving it again as the same name. I had it working, but the turnaround was closing and quitting every other open Excel file. This won't work for the users. Here's my code:
Public Sub RefreshPicks() On Error Resume Next Dim ObjXL As Excel.Application Set ObjXL = GetObject("C:\Users\CurrentSheet.xlsm", "Excel.Application") If Not (ObjXL Is Nothing) Then Debug.Print "Closing XL" ObjXL.Application.DisplayAlerts = False ObjXL.Workbooks.Close ObjXL.Quit Else Debug.Print "XL not open" End If Dim sPath As String, xlFile As String sPath = "G:\XE_ECMs\__MOST_REPORT\Reservation Data\Refresh\" xlFile = Dir(sPath & "*.xlsm") Dim XL As Excel.Application, wbTarget As Workbook Dim qdfResults As QueryDef Dim qdfResults2 As QueryDef Dim qdfResults3 As QueryDef Dim qdfResults4 As QueryDef Dim rsResults As DAO.Recordset Dim rsResults2 As DAO.Recordset Dim rsResults3 As DAO.Recordset Dim rsResults4 As DAO.Recordset Set XL = New Excel.Application Set wbTarget = XL.Workbooks.Open("G:\XE_ECMs\__MOST_REPORT\Reservation Data\PickListTemplate.xlsm") Set qdfResults = CurrentDb.QueryDefs("OpenRsrvtnsQ") Set rsResults = qdfResults.OpenRecordset() Set qdfResults2 = CurrentDb.QueryDefs("TrackingQ") Set rsResults2 = qdfResults2.OpenRecordset() Set rsResults3 = CurrentDb.OpenRecordset("FactoryDeletedT") Set qdfResults4 = CurrentDb.QueryDefs("WorkingTasksQ") Set rsResults4 = qdfResults4.OpenRecordset() Debug.Print xlFile'Process DoCmd.SetWarnings False DoCmd.RunSQL "DELETE * FROM ProcessingT" DoCmd.RunSQL "DELETE * FROM WorkingTasksT" DoCmd.TransferSpreadsheet acImport, 10, "ProcessingT", sPath & xlFile, True, "PickList!A2:V5000" DoCmd.TransferSpreadsheet acImport, 10, "WorkingTasksT", sPath & xlFile, True, "WorkingTasks!A1:V5000" DoCmd.OpenQuery "RemoveProcessingDuplicatesQ" DoCmd.OpenQuery "UpdatePicksQ" DoCmd.OpenQuery "UpdateClosePicksQ" DoCmd.OpenQuery "UpdateWorkingTasksQ" DoCmd.OpenQuery "CloseRsrvtnsInWHtransTQ" DoCmd.OpenQuery "PurgeClosedInProcessingTQ" DoCmd.OpenQuery "UpdateSlocQ" If DCount("*", "OffListQ") > 0 Then DoCmd.OpenQuery "FactoryDeletedQ" DoCmd.OpenQuery "CloseFactoryDeletedQ" Else: End If If DCount("*", "TrackedTodayQ") = 0 Then Call Snapshot 'Appends TrackingT data Else: End IfXL.Visible = True wbTarget.Sheets("PickList").Range("A3").CopyFromRecordSet rsResults Set rsResults = Nothing XL.Run "SetFormatting" wbTarget.Sheets("Tracking").Range("A2").CopyFromRecordSet rsResults2 Set rsResults2 = Nothing wbTarget.Sheets("FactoryDeleted").Range("A3").CopyFromRecordSet rsResults3 Set rsResults3 = Nothing wbTarget.Sheets("WorkingTasks").Range("A2").CopyFromRecordSet rsResults4 Set rsResults4 = Nothing XL.Run "SetFormatting" Set wbTarget = Nothing'ClearProcessingT DoCmd.SetWarnings True XL.Run "SaveAll" Access.QuitEnd Sub
Which workbook do you want to close and when do you want to close it?
Jay Williams
ASKER
C:\Users\CurrentSheet.xlsm as soon as it starts Access. Do I need to split the Access code? Use one procedure to close C:\Users\CurrentSheet.xlsm and then call the rest of it in another procedure?
Norie
If you just wanted to close that specific workbook you could use this.
ObjXL.Workbooks("CurrentSheet.xlsm").Close SaveChanges:=False ' or True