I am trying to write the vb code to clear any and all excel applications which may be running on the PC.
I have the following but keep getting an error when it goes to close out the second app.
Then I get it to close (the windows disappear from the ststus bar) but when I open TAsk Manager it still shows EXCEL.EXE as still active.
CAn someone correct this code so that when it is run all Excel applicationsa re correctly closed AND no EXCEL.EXE tasks are running?
I think I am close it is just a matter of executing the correct excel syntax -- Im still a learner in Excel VBA
Option Compare Database
Function ClearAllRunningExcelWorkbo
oks()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
StartClearExcel:
Set oXL = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
GoTo EndClearExcelRoutine ' added
' Set oXL = New Excel.Application ' removed
End If
On Error GoTo Err_Handler
'If you want Excel to be visible, you could add the line: oXL.Visible = True here; but your code will run faster if you don't make it visible
'Open the workbook
'Process each of the spreadsheets in the workbook
Set oWB = oXL.ActiveWorkbook
For Each oSheet In oXL.ActiveWorkbook.Workshe
ets
'put guts of your code here
'get next sheet
Set oSheet = Nothing
Next oSheet
oWB.Quit
Set oWB = Nothing
oXL.Quit
Set oXL = Nothing
'Next oWB
If ExcelWasNotRunning Then
oXL.Quit
End If
ExcelWasNotRunning = False
'Next
GoTo StartClearExcel
EndClearExcelRoutine:
'Make sure you release object references.
Set oRng = Nothing
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
'quit
Exit Function
Err_Handler:
MsgBox Err.Description, vbCritical, "Error: " & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If
Start Free Trial