_Communicating between 2 XL instances_ !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,596
Office Version
  1. 2016
Platform
  1. Windows
I have two instances of XL running at the same time.

Instance A has workBook A loaded.

Instance B has WorkBook B loaded.

How do I get ,via code, the current value of Range("A1") in Sheet1 in Workbook B from WorkBook A ?

Any idea how to best achieve this ?

Regards.
 
From your first post it appears you can use
Set objXLInstance = GetObject("C:\B.XLS")
Correct?

If so, once you get the *title* of the other instance of XL from GetWindowText, use something like
Set objXLInstance = GetObject({title of other XL instance})?

The only other alternative I can think of is to use a VBScript (basically something that is not XL-based) to first use GetObject and find the first running instance, then open a new instance of XL, open the appropriate workbook and call a subroutine in the workbook/add-in that will accept an object as an argument. The VBScript code will pass the variable of the first instance of XL as this argument. Something along the lines of:

Code:
set x=GetObject("Excel")
set y=CreateObject("Excel")
y.workbooks.open("{file of interest}")
y.run "'{xla or xls filename}'!getOtherXLInstance", x

and in the workbook/add-in:
Code:
dim global2ndInstance as object
public sub getOtherXLInstance(byval x as object)
    set global2ndInstance=x
    end sub
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
tusharm said:
From your first post it appears you can use
Set objXLInstance = GetObject("C:\B.XLS")
Correct?

If so, once you get the *title* of the other instance of XL from GetWindowText, use something like
Set objXLInstance = GetObject({title of other XL instance})?

No, GetObject returns the B.xls workbook only if the fully qualified file path of the workbook is hard coded and passed as its first argument .

What I wanted is to be able to get the reference without necessarly knowing the path of the workbook that is loaded in the seconde instance of XL...Kind of generic code.

Passing the caption of the second XL App woudn't work.

Regards.
 
Upvote 0
It's not going to be that easy Tusharm and that is why I have yet to find a single example outside of using C. However, from what I have seen so far, I am not at all convinced that this cannot be done using VB.
First of all it is impossible to differentiate between instances of Excel because the application only registers it'self once irregardless of how many instances are running. However, Excel also registers each workbook in the ROT (Running Object Table). The goal Jafaar is after is to reference an existing instance of Excel by simply passing a workbook name without knowing the path. Using GetObject, this syntax works with "some/any instance" that contains a workbook that has yet to be saved and will return the correct App instance.

Set AppInstance = GetObject("Book1").Application

The reason being that "Book1" has been registered in the ROT with the string "Book1". However, once "Book1" has been saved, you will now need the full path to get the same results. Herein lies the problem.

See this page for more info...

Here is a solution is C.

If you are interested in a Net solution I'll help you out Jafaar. I'm not going to spend any more time on this one because I have never run into the need to reference a specific app instance and it seems that you should instantiate it yourself. Why do your need to do this anyway???

Tom
 
Upvote 0
Right_Click said:
It's not going to be that easy Tusharm and that is why I have yet to find a single example outside of using C. However, from what I have seen so far, I am not at all convinced that this cannot be done using VB.
First of all it is impossible to differentiate between instances of Excel because the application only registers it'self once irregardless of how many instances are running. However, Excel also registers each workbook in the ROT (Running Object Table). The goal Jafaar is after is to reference an existing instance of Excel by simply passing a workbook name without knowing the path. Using GetObject, this syntax works with "some/any instance" that contains a workbook that has yet to be saved and will return the correct App instance.

Set AppInstance = GetObject("Book1").Application

The reason being that "Book1" has been registered in the ROT with the string "Book1". However, once "Book1" has been saved, you will now need the full path to get the same results. Herein lies the problem.

See this page for more info...

Here is a solution is C.

If you are interested in a Net solution I'll help you out Jafaar. I'm not going to spend any more time on this one because I have never run into the need to reference a specific app instance and it seems that you should instantiate it yourself. Why do your need to do this anyway???

Tom


Tom,

Sorry to keep nagging you with this . I don't really need this for a project or something, I am just doing it for the sake of learning and to get an insight into the inner workings of Windows applications.

Thanks for the attached links.


Regards.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top