_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.
 
The code worked in every way I could concieve of. A new workbook that had not been saved, a new workbook that had been saved but never closed, and a workbook that had been saved, closed, and then reopened. The behavior differed for each. However, it did do as advertised by passing a reference to the correct instance. In what way is it not working for you? Please explain...

I searched for the post Tusharm was speaking of and could not find it. I did find an example but the developer is reponsible for starting each instance of Excel himself.

See the framework's Marshal Class for a possible net solution.
See this page for some info on which API functions are called by GetObject...
Look here for various COM API functions...

I found a handful of posts from various boards that contained similiar requirements as yours. None of them were answered satisfactory... I would really like to see the post that Tusharm was talking about. Keep in mind, Tusharm, that we are looking for existing instances that the developer had no part in initiating...

Tom
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Tom,

I keep getting "Not located or does not exist..." when I assign the workbook name to the WorkbookShortName variable in the example procedure !

Sometimes I get the one of the Recently opend files instead !

I haven't followed the code properly so I may be missing something.

Thanks.
 
Upvote 0
Tom,

Thank you for the various links :)

the stuff inside looks promising but quite intimidating and I don't know anything about COM at this stage but I am curious to learn about it & other related subjects.I can see the enormeous potential.

Do you have any suggestions as to how/if I should invest in learning this ?

Regards.
 
Upvote 0
Just my opinion. I would focus on net. If I were to create a solution that you are looking for, I would check out the Marshall class. The limitation involved would be that the Net Framework needs to be installed on the PC. MS could be wrong, but COM should eventually, albeit slowly, phase out. Atleast MS hopes so. Nevertheless, Microsoft thought the same thing about DDE. If you are going to program COM you are not going to do it in VB but in a lower level language. From what I found searching and according to what Tusharm has claimed, you can accompish your goal from VB/VBA using the COM Apis. You will need to learn to create IUnknown variables in VB which I don't have a clue but I know it can be done. Look at the C/C++ examples on the net, the functions are recognizable. I just can't find a good enough reason. I checked out your scrollbar post and it appears that the only way to send a SetProperty message to Excel's scrollbars would be through subclassing and intercepting the messages sent to the parent window. It would be easier to literally create and register your own scrollbars using straight API functions. We all know that subclassing Excel in VBA = VBno... :)

Tom
 
Upvote 0
Tom,

Thanks for the info.

Regarding the Scrollbars post, do you have any sample code at all ? even if it doesn't work at least it can give me an idea and hopefully I can build on it.

I have used the SetBkColor API on the scrollbars DC but I can't get it to work .

Regards.
 
Upvote 0
Untested suggestions:

1) Use EnumWindows to get a list of handles to all running processes/applications. For each, in the callback procedure, use GetWindowText to get the window title (caption). Use that to figure out if it is the window of interest.

2) Ivan Moala has an example on his site of how to get a list of class names / captions for all running processes. Check out http://www.xcelfiles.com/API_06.html

Edit: Addition:

3) Also check Stephen Bullen's EnumDlg.zip at http://www.oaltd.co.uk/Excel/SBXLPage.asp#TheySaid
 
Upvote 0
tusharm said:
Untested suggestions:

1) Use EnumWindows to get a list of handles to all running processes/applications. For each, in the callback procedure, use GetWindowText to get the window title (caption). Use that to figure out if it is the window of interest.

2) Ivan Moala has an example on his site of how to get a list of class names / captions for all running processes. Check out http://www.xcelfiles.com/API_06.html

Edit: Addition:

3) Also check Stephen Bullen's EnumDlg.zip at http://www.oaltd.co.uk/Excel/SBXLPage.asp#TheySaid


Tusharm,

Thanks for the suggestions.

I know how to get the Window handle and Process ID of the second XL appllication (and all other running apps for that matter) using API functions, but don't know how to create a reference to it..so I could call it using a syntax like :

Set objXLSecondInstance=XLapplications(Instance2)

By setting a reference as shown above, one could easily use automation and manipulate all the objects in the second XL instane.

The real question is how to get a reference to a foreign process from it's Window hwnd or Process ID !

I have checked Stephen Bullen' s website as well as that of Ivan's but I couldn't find an answer to this problem.

Regards.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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