B
Bill Murphy
I'm using the following code (from the help file VBAXL10.chm) within Access
to determine whether Excel was running when I first opened the workbook in
VBA:
ExcelRunning = IsExcelRunning()
If Not ExcelRunning Then
Set objExcel = CreateObject("Excel.Application")
Else
Set objExcel = GetObject(, "Excel.Application")
End If
If Excel was running I leave it open, otherwise I quit Excel:
Function IsExcelRunning() As Boolean
' To use a pre-existing instance of Microsoft Excel with Automation, use
the GetObject function
' and specify "Excel.Application" as the Class type. If an instance of
Microsoft Excel already exists,
' the GetObject function will return a reference to the instance. If an
instance of Microsoft Excel
' does not already exist, your code will cause a trappable run-time
error, and you can use the
' CreateObject function to create one.
' You can use the following function to determine if an instance of
Microsoft Excel is running.
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
This function sometimes returns true, even if Excel was not running when it
was called. Is there a way to fix this code, or is there a better way to
make this determination?
Bill
to determine whether Excel was running when I first opened the workbook in
VBA:
ExcelRunning = IsExcelRunning()
If Not ExcelRunning Then
Set objExcel = CreateObject("Excel.Application")
Else
Set objExcel = GetObject(, "Excel.Application")
End If
If Excel was running I leave it open, otherwise I quit Excel:
Function IsExcelRunning() As Boolean
' To use a pre-existing instance of Microsoft Excel with Automation, use
the GetObject function
' and specify "Excel.Application" as the Class type. If an instance of
Microsoft Excel already exists,
' the GetObject function will return a reference to the instance. If an
instance of Microsoft Excel
' does not already exist, your code will cause a trappable run-time
error, and you can use the
' CreateObject function to create one.
' You can use the following function to determine if an instance of
Microsoft Excel is running.
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
This function sometimes returns true, even if Excel was not running when it
was called. Is there a way to fix this code, or is there a better way to
make this determination?
Bill