vuyy1182
vuyy1182

Reputation: 1686

Excel workbook closed through vba code still running in the process

Through Access VBA i'm opening the workbook and make visible=False, and then i do something and closing. But it is still running in the Taskmanager.

     Dim ExcelApp As New Excel.Application
     Dim ExcelBook As New Excel.Workbook
     Dim rng As Excel.Range
     Dim rngDefine As Excel.Range

     Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
     ExcelApp.Visible = False       

     '//Do something

    ExcelApp.Quit
    ExcelBook.Close SaveChanges:=False
    Set ExcelBook = Nothing
    Set ExcelApp = Nothing         

Upvotes: 1

Views: 3214

Answers (1)

MattB
MattB

Reputation: 2241

The commenters above are correct, your new workbook isn't associated with your ExcelApp instance. So at the end you're closing your new ExcelApp instance and closing the new workbook you created, but I think the new workbook is opening in a new instance of Excel... maybe, so you'd still see an orphaned Excel process in the task manager. I'd change your code to something more like this:

Dim ExcelApp as Excel.Application
Dim ExcelBook as Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range
Set ExcelApp = New Excel.Application
Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
'ExcelApp.Visible = False   You could maybe comment this out unless you use ".Activate" somewhere.  It should be the default anyway.
...
'At the end:
ExcelApp.DisplayAlerts = False
ExcelBook.Close
ExcelApp.DisplayAlerts = True
ExcelApp.Quit
'If right before "End Sub" the next two lines are of arguable necesity, but likely good practice
Set ExcelBook = Nothing
Set ExcelApp = Nothing

Upvotes: 2

Related Questions