I use the following code to determine if there is a hidden instance of Excel running. I first determine if there is an active Excel instance. If so, I then determine if the instance is hidden.
If I identify that there is an active hidden instance of Excel (this function returns a value of 2), how can I close it?
Here is the code to test that Excel is running:
If I identify that there is an active hidden instance of Excel (this function returns a value of 2), how can I close it?
Code:
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Const Err_App_Notrunning As Long = 429
On Error GoTo ErrorHandling:
If isExcelOpen Then
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err = Err_App_Notrunning Then
Set xlApp = New Excel.Application
End If
If xlApp.Visible = True Then
ExcelVerifiedClosed = 1
Else
ExcelVerifiedClosed = 2
End If
Else
ExcelVerifiedClosed = 0
End If
ExitProc:
Exit Function
ErrorHandling:
Select Case Err.Number
Case Else
Call ErrorHandler("ExcelVerifiedClosed", Err.Number, Err.Description)
GoTo ExitProc
End Select
Here is the code to test that Excel is running:
Code:
Public Function isExcelOpen() As Boolean
Dim xlApp As Object
On Error GoTo ErrorHandling:
'Initialize Return and variables
isExcelOpen = False
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number = 0 Then 'excel open
isExcelOpen = True
Err.Clear
Else
xlApp.Quit
Set xlApp = Nothing
End If
ExitProc:
Exit Function
ErrorHandling:
Select Case Err.Number
Case Else
Call ErrorHandler(msModuleName & ".isExcelOpen", Err.Number, Err.Description)
GoTo ExitProc
End Select
End Function