here's my original post VBA Excel instance doesn't close when opened from MS Access - late binding it was answered and i didn't realize that the xlsm file still isn't closed when I accepted the answer.
The reason why this is such a huge problem, is because when there's an open ghost .xlsm file, the next .xlsm file that opens doesn't run the startup macro. so, all my automations are failing because the previous .xlsm is sitting in memory.
So, I have many automations scheduled to run. They're all similar in a way that an MS Access program is run, then, from the MS Access program the .xlsm is opened, then the MS Access program emails the results. More than 1 automation can be running at the same time, so, sometimes when the .xlsm is opened, there might be other excel files open, or other .xlsm files, or none at all.
What's happening is that, for some reason, using the code below, the .xlsm file doesn't close fully. It's not visible in the taskbar and the only reason I know that it's still opened somewhere is by going into the VBA Projects window (see the screenshot below).
Here's a screenshot that shows whats on my taskbar and what's in the VBA Projects.
And, for some reason, when the file is sitting like this, ghosted, the next .xlsm file doesn't run the startup macro. If i kill Excel, and then open the .xlsm file, the startup macro runs fine.
Here's the code I'm using
This is what opens the excel file from MS Access
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelWbk = ExcelApp.Workbooks.Open(CurPath & "CurrentProjects.xlsm", True)
ExcelApp.Visible = False
ExcelApp.Quit
Set ExcelWbk = Nothing
Set ExcelApp = Nothing
This is the startup macro. This is placed in the THIS WORKBOOK module.
Private Sub Workbook_Open()
CreateReporting
End Sub
Here's the code in the excel file
Option Explicit
Public Sub CreateReporting()
Dim MonthlyPath As String, CurPath As String, ProjectDate As String, FullName As String, CurLastRow As Long, CurRowNum As Long
Dim i As Range, CurCell As Variant, CurRange As Range
Dim wbkM As Workbook, wbkNewFile As Workbook, wksReportDates As Worksheet, wksCopyFrom As Worksheet, wksCopyTo As Worksheet, wks3 As Worksheet
Dim rngCopyFrom As Range, rngCopyTo As Range
CurPath = ThisWorkbook.Path & "\"
With ThisWorkbook.Sheets("QReportDates")
MonthlyPath = .Range("A2").Value
ProjectDate = .Range("B2").Value
End With
FullName = ProjectDate & " Current Projects.xlsx"
Set wbkM = Workbooks("CurrentProjects.xlsm")
Set wksReportDates = wbkM.Sheets("QReportDates")
If Dir(MonthlyPath, vbDirectory) = "" Then
MkDir (MonthlyPath)
End If
Set wbkNewFile = Workbooks.Add
wbkNewFile.SaveAs MonthlyPath & FullName
Set wbkNewFile = Workbooks(FullName)
wbkM.Sheets("TEMPLATEReporting").Copy after:=wbkNewFile.Sheets(1)
wbkNewFile.Sheets(2).Name = "Current Projects"
With wbkNewFile
.Sheets("Sheet1").Delete
End With
Set wksCopyFrom = wbkM.Sheets("QCurrentProjects")
Set wksCopyTo = wbkNewFile.Sheets("Current Projects")
With wksCopyFrom
CurLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set rngCopyFrom = .Range("A2:K" & CurLastRow)
End With
With wksCopyTo
Set rngCopyTo = .Range("A2:K" & CurLastRow)
End With
rngCopyTo.Value = rngCopyFrom.Value
For Each i In wksCopyTo.Range("F2:F" & CurLastRow)
With Worksheets(1)
.Hyperlinks.Add Anchor:=Range("F" & i.Row), Address:=Range("F" & i.Row), TextToDisplay:=Range("C" & i.Row).Value
End With
Next
wbkNewFile.Save
wbkNewFile.Close
Set wbkNewFile = Nothing
Set wksCopyTo = Nothing
Set rngCopyTo = Nothing
Set wksCopyFrom = Nothing
Set rngCopyFrom = Nothing
wbkM.Worksheets("QReportDates").Delete
wbkM.Worksheets("QCurrentProjects").Delete
wbkM.Save
wbkM.Close
Set CurCell = Nothing: Set CurRange = Nothing: Set wbkM = Nothing: Set wbkNewFile = Nothing: Set wksReportDates = Nothing: Set wksCopyFrom = Nothing: Set wksCopyTo = Nothing
End Sub
Note that in this version I'm closing the .xlsm file from excel
wbkM.Close
But I also tried closing it from MS Access and removing the wbkM.Close from Excel. Same result.
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelWbk = ExcelApp.Workbooks.Open(CurPath & "CurrentProjects.xlsm", True)
ExcelApp.Visible = False
ExcelWbk.Close
ExcelApp.Quit
Set ExcelWbk = Nothing
Set ExcelApp = Nothing