MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Open/Close Excel using VBA

Posted by John on May 09, 2000 12:08 PM

I'd like to open Excel using code, but only if it isn't already opened. I've got the code to open excel to work, but I now need to check for multiple running copies. Any ideas on checking to see excel is already open? I also need some code to close excel once i'm finished. Any help would be appreciated.

Posted by Ivan Moala on May 12, 2000 12:01 AM

Code to determine if excel is running
taken from Automation help file.

Function IsExcelRunning() As Boolean
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
IsExcelRunning = (Err.Number = 0)
Set xlApp = Nothing
End Function

And then, your code can determine whether it needs to create a new instance or notÂ…

Sub ExcelInstance()
Dim xlApp As Excel.Application

Dim ExcelRunning As Boolean

ExcelRunning = IsExcelRunning()
If ExcelRunning Then
Set xlApp = GetObject(, "Excel.Application")
Set xlApp = CreateObject("Excel.Application")
End If
'Other automation code here...

If Not ExcelRunning Then xlApp.Quit
Set xlApp = Nothing
End Sub


Posted by John on May 12, 2000 7:27 AM

Dim appExcel As Excel.Application
On Error Resume Next
Set appExcel = GetObject(,"Excel.Application")
IF appExcel Is Nothing Then 'No instance of Excel is available
Set appExcel = CreateObject("Excel.Application")
End If

'Then your code which manipulates the excel instance.

This code eliminates the Error Function and simplifies.

Posted by Ivan Moala on May 13, 2000 3:01 AM