Advertisement

01.12.2008 at 06:23PM PST, ID: 23078589
[x]
Attachment Details

VB Code to clear all open Excel workbooks - close excel

Asked by donaldmaloney in Microsoft Excel Spreadsheet Software, VB Database Programming

Tags:

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 ClearAllRunningExcelWorkbooks()

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.Worksheets
   '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
[+][-]01.12.2008 at 10:39PM PST, ID: 20646683

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.13.2008 at 07:59AM PST, ID: 20647976

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.13.2008 at 02:24PM PST, ID: 20649537

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, VB Database Programming
Tags: clear excel workbooks
Sign Up Now!
Solution Provided By: MrBullwinkle
Participating Experts: 1
Solution Grade: A
 
 
[+][-]01.13.2008 at 07:14PM PST, ID: 20650508

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628