Ok, that is due to the orphaned instance of Excel that get instantiated when calling the Workbook. Will need to declare those objects in the code.
Modifying the code from the other forum, like so. The error handling code ensures that the Excel instances will be closed in the event of error, much like the try-catch-finally block in .Net and Java languages.
Code not tested though, so if there's error, gotta debug them. But that's the basic idea.
Code:
Sub CloseNamed()
Const strWbkName As String = "ABook.xls"
Dim xlApp As Excel.Application
Dim xlWkBk As Excel.Workbook
On Error Goto ERR_HANDLER
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False 'Hide the application
If IsWorkbookOpen(strWbkName) Then
Set xlWkBk = xlApp.Workbooks(strWbkName)
xlWkBk.Close savechanges:=False
Else
'Open it and read the file and do something with the data
Set xlWkBk = xlApp.Workbooks.Open(FileName:=strWbkName, ReadOnly:=True)
End If
EXIT_CLEANUP:
'Cleanup code
On Error Resume Next
xlWkBk.Close False
Set xlWkBk = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
ERR_HANDLER:
'Error handling code here...
Resume EXIT_CLEANUP
End Sub
Function IsWorkbookOpen(stName As String) As Boolean
Dim xlApp As Excel.Application
Dim xlWkBk As Excel.Workbook
On Error Goto ERR_HANDLER
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False 'Hide the application
Set xlWkBk = xlApp.Workbooks(stName)
If Not Wkb Is Nothing Then IsWorkbookOpen = True
'Boolean Function assumed To be False unless Set To True
EXIT_CLEANUP:
'Cleanup code
On Error Resume Next
Set xlWkBk = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Function
ERR_HANDLER:
'Error handling code here...
IsWorkbookOpen = True
Resume EXIT_CLEANUP
End Function