I know that this has been hashed over many times but none of the solutions work for me
This runs from MS Access
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Workbooks.Open CurPath & MainProjectName & ".xlsm", True
ExcelApp.Visible = False
ExcelApp.Quit
Set ExcelApp = Nothing
Also, the .xlsm file does the following at the end of the procedure
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
but the .xlsm file remains open hidden somewhere. i see it as an instance, not as an application and the reason i know that the .xlsm file stays open because sometimes the excel VBA window stays open (just the VBA window, not the Excel window) and in there i can see which file's modules are there.
posting all my code
this is the piece that runs from MS Access and opens the xlsm file
Public Function RunLoadFilesTest()
ODBCConnString
RunVariables
Dim Rs2 As DAO.Recordset
Dim TABLENAME As String
Set Rs2 = CurrentDb.OpenRecordset("SELECT * FROM QFilesToExportEMail")
Do Until Rs2.EOF
TABLENAME = Rs2("TableName")
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, TABLENAME, CurPath & MainProjectName & ".xlsm", True
Rs2.MoveNext
Loop
Rs2.Close
Set Rs2 = Nothing
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelWbk = ExcelApp.Workbooks.Open(CurPath & MainProjectName & ".xlsm", True)
ExcelApp.Visible = False ' APP RUNS IN BACKGROUND
'ExcelWbk.Close ' POSSIBLY SKIP IF WORKBOOK IS CLOSED
ExcelApp.Quit
' RELEASE RESOURCES
Set ExcelWbk = Nothing
Set ExcelApp = Nothing
End Function
this is the code of the xlsm file. it opens automatically from the ThisWorkbook module. i removed a lot of the code not to clutter the thread but left every piece that opens a workbook, activates a workbook, closes, etc.
Public Sub MainProcedure()
Application.EnableCancelKey = xlDisabled
Application.DisplayAlerts = False
Application.EnableEvents = False
CurPath = ActiveWorkbook.Path & "\"
'this is to deselect sheets
Sheets("QFilesToExportEMail").Select
Sheets("QReportDates").Activate
FormattedDate = Range("A2").Value
RunDate = Range("B2").Value
ReportPath = Range("C2").Value
MonthlyPath = Range("D2").Value
ProjectName = Range("E2").Value
Windows(ProjectName & ".xlsm").Activate
Sheets("QFilesToExportEMail").Select
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim i As Integer
CurRowNum = 2
Set CurRange = Sheets("QFilesToExportEMail").Range("B" & CurRowNum & ":B" & LastRow)
For Each CurCell In CurRange
If CurCell <> "" Then
Windows(ProjectName & ".xlsm").Activate
Sheets("QFilesToExportEMail").Select
FirstRowOfSection = ActiveWorkbook.Worksheets("QFilesToExportEMail").Columns(2).Find(ExcelFileName).Row
If ExcelSheetName = "" Then
ExcelSheetName = TableName
End If
If CurRowNum = FirstRowOfSection Then
SheetToSelect = ExcelSheetName
End If
If IsNull(TemplateFileName) Or TemplateFileName = "" Then
Workbooks.Add
Else
Workbooks.Open CurPath & TemplateFileName
End If
ActiveWorkbook.SaveAs MonthlyPath & FinalExcelFileName
For i = CurRowNum To LastRowOfSection
Windows(ProjectName & ".xlsm").Activate
Sheets("QFilesToExportEMail").Select
Next i
End If
Windows(FinalExcelFileName).Activate
Sheets(SheetToSelect).Select
ActiveWorkbook.Save
ActiveWorkbook.Close
If LastRowOfSection >= LastRow Then
Exit For
End If
Next
Set CurRange = Sheets("QFilesToExportEMail").Range("A2:A" & LastRow)
For Each CurCell In CurRange
If CurCell <> "" Then
CurSheetName = CurCell
If CheckSheet(CurSheetName) Then
Sheets(CurSheetName).Delete
End If
End If
Next
Sheets("QFilesToExportEMail").Delete
Sheets("QReportDates").Delete
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub