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
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.