GetObject when more than 1 Excel is running

E

Ed White

From within VB.Net, if I want to access an instance of Excel that is already
running, I'd use something like.
dim Exc as Excel.Application
Exc = GetObject(, "Excel.Application")

However, suppose I have more than one instance of Excel open. Is there a
way to cycle through each open instance, and to pick a specific one to open?
 
J

Jialiang Ge [MSFT]

Hello Ed,

A few days ago, I helped a customer with a similar question ¡°how to
automation multiple Excel instances¡± at this thread:

http://www.microsoft.com/communitie...32-a524-fe6a711b8409&cat=&lang=&cr=&sloc=&p=1
Here is an archive link:
http://www.eggheadcafe.com/software/aspnet/32483244/more-about-trying-to-auto.aspx

Note: I made a mistake in the initial reply of the thread and explained the
reason in the follow-ups that the method 2
¡°IRunningObjectTable::EnumRunning¡±, in fact, does not work for Excel¡±.
The only solution is to use the method 1 ¡°use BindToMoniker with the name
of the file that is opened in that instance.¡±, namely, File Moniker. The
code GetObject(, "Excel.Application") is only able to return the first
instance of Excel. Ed, would you please refer to that thread and let me know
whether the information is helpful to you or not? If you have any other
questions or concerns, please DON¡¯T hesitate to tell me.

Regards,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Doug Glancy

Ed,

Check out this page:
http://support.microsoft.com/kb/288902

Here's the relevant part:
You can attach to a specific instance if you know the name of an open
document in that instance. For example, if an instance of Excel is running
with an open workbook named Book2, the following code attaches successfully
to that instance even if it is not the earliest instance that was launched:
Set xlApp = GetObject("Book2").Application

Depending on what you're doing you might consider using CreateObject instead
and opening the file you are interested in. I think that's generally
considered a safer practice.
hth,

Doug
 
P

Peter T

As already suggested, where the filename is known use GetObject to reference
the workbook then attach to the parent application.

For situations where there is no known loaded wb, Jialiang Ge appears to
have demonstrated how to find multiple instances with .net

For VBA, I have two methods, briefly:

The first enumerates Excel windows, then workbook windows to find the file
name of a loaded wb. Having got that try GetObject > parent. If that fails,
eg no loaded workbooks at all or some other reason, use DDE (from a helper
app) to add a new dummy hidden wb and try again.

The other way is finding in the ROT (clever stuff, not mine!) There are
dis/advantages with each. Via the ROT is faster but can't get be sure to get
all instances, eg no loaded wb as can with DDE (usually!).

If anyone is interested I have a demo that implements both methods. Having
got the ref's the withevents xlApp classes can track and record changes to
cells in all wb's in all instances, and one or two other things.

Regards,
Peter T
pmbthornton gmail com
 
O

Obaid Ullah

Can you share code that enumerates multiple Excel windows.



Peter T wrote:

As already suggested, where the filename is known use GetObject to
23-Jul-08

As already suggested, where the filename is known use GetObject to referenc
the workbook then attach to the parent application

For situations where there is no known loaded wb, Jialiang Ge appears t
have demonstrated how to find multiple instances with .ne

For VBA, I have two methods, briefly

The first enumerates Excel windows, then workbook windows to find the fil
name of a loaded wb. Having got that try GetObject > parent. If that fails
eg no loaded workbooks at all or some other reason, use DDE (from a helpe
app) to add a new dummy hidden wb and try again

The other way is finding in the ROT (clever stuff, not mine!) There ar
dis/advantages with each. Via the ROT is faster but can't get be sure to ge
all instances, eg no loaded wb as can with DDE (usually!)

If anyone is interested I have a demo that implements both methods. Havin
got the ref's the withevents xlApp classes can track and record changes t
cells in all wb's in all instances, and one or two other things

Regards
Peter
pmbthornton gmail co


alread
open?

Previous Posts In This Thread:

GetObject when more than 1 Excel is running
From within VB.Net, if I want to access an instance of Excel that is already
running, I'd use something like
dim Exc as Excel.Applicatio
Exc = GetObject(, "Excel.Application"

However, suppose I have more than one instance of Excel open. Is there a
way to cycle through each open instance, and to pick a specific one to open
--
Ed

Re: GetObject when more than 1 Excel is running
Ed

Check out this page
http://support.microsoft.com/kb/28890

Here's the relevant part
You can attach to a specific instance if you know the name of an open
document in that instance. For example, if an instance of Excel is running
with an open workbook named Book2, the following code attaches successfully
to that instance even if it is not the earliest instance that was launched
Set xlApp = GetObject("Book2").Applicatio

Depending on what you're doing you might consider using CreateObject instead
and opening the file you are interested in. I think that's generally
considered a safer practice
hth

Dou


As already suggested, where the filename is known use GetObject to
As already suggested, where the filename is known use GetObject to referenc
the workbook then attach to the parent application

For situations where there is no known loaded wb, Jialiang Ge appears t
have demonstrated how to find multiple instances with .ne

For VBA, I have two methods, briefly

The first enumerates Excel windows, then workbook windows to find the fil
name of a loaded wb. Having got that try GetObject > parent. If that fails
eg no loaded workbooks at all or some other reason, use DDE (from a helpe
app) to add a new dummy hidden wb and try again

The other way is finding in the ROT (clever stuff, not mine!) There ar
dis/advantages with each. Via the ROT is faster but can't get be sure to ge
all instances, eg no loaded wb as can with DDE (usually!)

If anyone is interested I have a demo that implements both methods. Havin
got the ref's the withevents xlApp classes can track and record changes t
cells in all wb's in all instances, and one or two other things

Regards
Peter
pmbthornton gmail co


alread
open?


Submitted via EggHeadCafe - Software Developer Portal of Choice
Xcompress - IIS HTTP Compression
http://www.eggheadcafe.com/tutorial...c7-861a0909f404/xcompress--iis-http-comp.aspx
 
P

Peter T

One way -

Option Explicit
Private Declare Function FindWindowEx Lib "user32" Alias _
"FindWindowExA" ( _
ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long

Sub test()
Dim i As Long
Dim arrXLhWnd() As Long

If GetXLhWnds(arrXLhWnd) Then
For i = LBound(arrXLhWnd) To UBound(arrXLhWnd)
Debug.Print arrXLhWnd(i)
Next
End If

End Sub

Function GetXLhWnds(arrXLhWnd() As Long) as Long
Dim n As Long
Dim hWndXL As Long, hWndDT As Long

ReDim arrXLhWnd(1 To 100) ' cater for 100 potential Excelinstances
hWndDT = GetDesktopWindow

Do
hWndXL = FindWindowEx(hWndDT, hWndXL, "XLMAIN", vbNullString)
If hWndXL Then
n = n + 1
arrXLhWnd(n) = hWndXL
End If
Loop Until hWndXL = 0

If n Then
ReDim Preserve arrXLhWnd(1 To n)
GetXLhWnds = n
Else
Erase arrXLhWnd
End If

End Function

Regards,
Peter T
 
R

Roger

Thanks but trying

Set xlApp = GetObject("Book2").Application

.... does not work on multiple instances. Tested on windows 7 and excel 2003.

"Cannot create activex component"

I've checked and checked and checked it, but it is unable to see the opened excel.

More details: if the filename is "my book.xls" and is in "c:\here\", I try to open it as:

Set xlApp = GetObject("my book").Application

Am I missing something ? I cannot include the path as it changes very often...

Thanks in advance,

Roger
 
R

Roger Tranc hez

GetObject when more than 1 Excel is running

Thanks, but trying THIS:
Set xlApp = GetObject("Book2").Application
.... does not work on multiple instances. Tested on windows 7 and excel 2003.
The error I'm getting is "Cannot create activex component"
I've checked and checked and checked it, but it is unable to see the opened excel.
More details: if the filename is "my book.xls" and is in "c:\here\", I try to open it as:
Set xlApp = GetObject("my book").Application
Am I missing something ? I cannot include the path as it changes very often...
Thanks in advance,
Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top