Archive of Mr Excel Message Board

Back to Excel VBA archive index
Back to archive home

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.

| Check out our Excel VBA Resources
|
 |
 |
 |
 |
 |
Re: Open/Close Excel using VBA
Posted by Ivan Moala on May 12, 2000 12:01 AM
John
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
Err.Clear
End Function
then;
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")
Else
Set xlApp = CreateObject("Excel.Application")
End If
'Other automation code here...
If Not ExcelRunning Then xlApp.Quit
Set xlApp = Nothing
End Sub
Ivan

Re: Open/Close Excel using VBA
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.

Re: Open/Close Excel using VBA
Posted by Ivan Moala on May 13, 2000 3:01 AM

This archive is from the original message board at www.MrExcel.com.
All contents © 1998-2004 MrExcel.com.
Visit our
online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.