ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check for instance of EXCEL running (https://www.excelbanter.com/excel-programming/309776-check-instance-excel-running.html)

Kevin

Check for instance of EXCEL running
 
I'm trying to test for an existing instance of Excel already running.

In my VB6 code I do the following:

Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")

If Excel were already running I would do this:
Set oExcel = GetObject(, "Excel.Application")

How can I test to see if Excel is already running? I've found some examples
but they were a bit convoluted.

Thanks for any help.

Kevin



Kevin

Check for instance of EXCEL running
 
Thanks! That works. I used it like below: Now a followup question if I
may...
Looking in Task Manager, If an existing instanse of Excel was running, my
app will cause a secnod Excel process to run. It works fine, except when I
use code to reference workbooks and sheets for formatting. Can I use the
existing instance of Excel instead of opening Excel again? Is there a
variation of GetObject(, "Excel.Application") that will use the existing
instance of Excel?

Thanks very much.

Kevin

If FindWindow("XLMAIN", vbNullString) Then
Set oExcel = GetObject(, "Excel.Application")
Else
Set oExcel = CreateObject("Excel.Application")
End If

"AA2e72E" wrote in message
...
Try:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

Sub aa()
Debug.Print FindWindow("XLMAIN", vbNullString)
End Sub

This code must go in a module. If the result is 0, no Excel session is
running.
XLMAIN is the class name for ALL versions of Excel.
"Kevin" wrote:

I'm trying to test for an existing instance of Excel already running.

In my VB6 code I do the following:

Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")

If Excel were already running I would do this:
Set oExcel = GetObject(, "Excel.Application")

How can I test to see if Excel is already running? I've found some

examples
but they were a bit convoluted.

Thanks for any help.

Kevin






Harald Staff

Check for instance of EXCEL running
 
Hi Kevin

Don't overcomplicate this. Just use GetObject. If it errs then excel's not
running and so you create one:

Dim oExcel As Object
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If oExcel Is Nothing Then
Set oExcel = CreateObject("Excel.Application")
End If

HTH. Best wishes Harald

"Kevin" skrev i melding
...
I'm trying to test for an existing instance of Excel already running.

In my VB6 code I do the following:

Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")

If Excel were already running I would do this:
Set oExcel = GetObject(, "Excel.Application")

How can I test to see if Excel is already running? I've found some

examples
but they were a bit convoluted.

Thanks for any help.

Kevin





Tom Ogilvy

Check for instance of EXCEL running
 
Look in help at GetObject. See the help example. It pretty much shows all
you have asked.

--
Regards,
Tom Ogilvy

"Kevin" wrote in message
...
Thanks! That works. I used it like below: Now a followup question if I
may...
Looking in Task Manager, If an existing instanse of Excel was running, my
app will cause a secnod Excel process to run. It works fine, except when

I
use code to reference workbooks and sheets for formatting. Can I use the
existing instance of Excel instead of opening Excel again? Is there a
variation of GetObject(, "Excel.Application") that will use the existing
instance of Excel?

Thanks very much.

Kevin

If FindWindow("XLMAIN", vbNullString) Then
Set oExcel = GetObject(, "Excel.Application")
Else
Set oExcel = CreateObject("Excel.Application")
End If

"AA2e72E" wrote in message
...
Try:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA"

(ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

Sub aa()
Debug.Print FindWindow("XLMAIN", vbNullString)
End Sub

This code must go in a module. If the result is 0, no Excel session is
running.
XLMAIN is the class name for ALL versions of Excel.
"Kevin" wrote:

I'm trying to test for an existing instance of Excel already running.

In my VB6 code I do the following:

Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")

If Excel were already running I would do this:
Set oExcel = GetObject(, "Excel.Application")

How can I test to see if Excel is already running? I've found some

examples
but they were a bit convoluted.

Thanks for any help.

Kevin









All times are GMT +1. The time now is 05:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com