>>
APLDN Home

>>
Events

>>
Trainings

>>
APL Books

>>
APLDN Links

>>
Discussion Groups

>>
Downloads

>>
Articles

>>
Library

>>
Learning Tools

>>
APLDN User IO

>>
APL2000.com




General

Author Thread: Connect to Already-Running Excel
Rex.Swain
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 9:07 AM (EST)

I would like to find out whether Excel is running, and if so, start an ActiveX conversation with it.  (Specifically, I would like to know what workbooks it has open, and perhaps read some cells from an open workbook.  No writing.)  Does anyone know if this is possible?

 

My application can find what is probably a running instance of Excel with

FindFrame 'Microsoft Excel'

using FindFrame from the supplied WINDOWS workspace.  But couldn't it be fooled by, say, an open Word document name "Microsoft Excel.doc"?  So, question 1:  Is there a more definitive way to see if Excel is running?

 

And question 2:  supposing that Excel is running, can I use the window handle from above (or whatever evidence I have) to "connect" to that instance of Excel?


Comments:

Author Thread:
davin.church
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 12:45 PM (EST)

To connect to a running Excel, I expect that you'll have to have an Object pointer (to use with []WI).  I don't think that a Window Handle is that kind of a pointer, hence you probably can't use it at all.

 

I don't know of any way to find an object pointer to something else in memory.  Such a thing may not even exist.

     

Ken.Chakahwata
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 2:13 PM (EST)

hi rax, i'm new to apl+win so consider the following a stab in the dark for your question 1.

 

1/ find children of root with

'#' []wi 'children'

2/ find the one(s) with 'description' property  of

 'Microsoft Excel Application'

3/ for each you could also double check its class property is

'ActiveObject Excel.Application'

 

i have probably missed something -- so wud be interested in the 'correct' way.

 

regards

 

ken

     

davin.church
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 2:27 PM (EST)
I think he's asking to connect to a copy of Excel that was NOT started by APL.

     

Rex.Swain
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 2:30 PM (EST)
Yes, that's right, I am looking for an Excel instance that was not started by APL.

     

Ken.Chakahwata
Connect to Already-Running Excel
Posted: Tuesday, July 20, 2004 3:05 PM (EST)

hi rex,

 

ya, knew i'd missed something. i dont know whether there is a way APL can interact with an activeX object that it did not instantiate. but i vaguely seem to recall reading some discussions about this very issue in aVB discussion group -- if i find something thats promising here -- i'll pass it on. apologies for my first incorrect response...

 

regards

 

ken

     

brent.hildebrand
Connect to Already-Running Excel
Posted: Monday, July 26, 2004 4:42 AM (EST)
What I think you can do, is start APL from Excel, and pass APL, Excel's object pointer, then interact that way. Would that work for you?

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, July 26, 2004 7:54 AM (EST)
No, I need something that would detect Excel that the user may or may not have started, before or after APL was started.  APL and Excel are completely independent of each other.

     

Michael.Hughes
APL + Excel
Posted: Monday, July 26, 2004 10:11 AM (EST)

I'm only aware of three ways you can communicate between APL and Excel.

 

1. As already mentioned you have to add a small macro which loads when the sheet loads (I think the name is LoadPage but not sure of name).

This starts an APL session and passes it the pointer to the Excel object. This can then process all Excel events or pass them back to Excel.

2. DDE communication

3. Using ODBC

 

However in the last two you need to know thge correct protocol.

 

However as long as you can find a  way of passsing the Excel pointer back to an APL task it should be possible.   

   

     

Support
Connect to Already-Running Excel
Posted: Monday, July 26, 2004 3:12 PM (EST)

1.  Is there a more definitive way to see if Excel is running?

Yes.  Follow the example below:

 

      Œ„hwnd„FindFrame 'Microsoft Excel'
263240
      Œ„procid„2œŒwcall 'GetWindowThreadProcessId' hwnd Ð
740
      Œ„proc_hwnd„ŒWCALL 'OpenProcess' 'PROCESS_QUERY_INFORMATION PROCESS_VM_READ' 0 (†procid)
524
      2œŒwcall 'GetModuleFileNameEx' proc_hwnd 0 (128½Œtcnul) 128
D:\PROGRA~1\MICROS~2\Office\EXCEL.EXE

 

Note that when two or more Excel applications are running, the FindFrame function will return the handle of the most recent active Excel application.

 

 

2.   Supposing that Excel is running, can I use the window handle from above (or whatever evidence I have) to "connect" to that instance of Excel?

No.  The proper way to do this is to have APL+Win "connect" to Excel via the COM interface.  You can connect APL+Win to an Excel session that is already running (not started by APL+Win) by simply starting from a different place in the Excel object hierarchy.  When you create an Excel.Application object, you are requesting a new instance of Excel to be started.  However, if Excel is already running and you instead create an Excel.Sheet object, you will be connected to that instance of Excel (and not create a new instance).  From the Excel.Sheet object, you then obtain the object pointer to Excel’s Application object using the xApplication property.  For example:

 

    'xlsheet' ŒWI 'Create' 'Excel.Sheet'
    'xlsheet' ŒWI 'Application > xlapp'
    'xlsheet' ŒWI 'Delete'

 

You now have an "xlapp" object that is connected to an Excel.Application object.  From here you invoke the actions necessary to query the worksheet for the desired data.
 

I hope this information helps.

 

APL2000 Support

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 11:56 AM (EST)

That works great, John; thanks very much.

 

There is one remaining detail, if you want to go for extra credit...  If there is more than one instance of Excel running, 'xApplication > xlapp' seems to connect to the oldest instance (but I wouldn't want to count on that). 

 

Is there a way to get control of which instance we connect to?

     

davin.church
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 12:31 PM (EST)
I thought that one running copy of the Excel application was shared among all the windows/sheets that were open, and that's why the above trick works.  Is this not (or no longer) the case?

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 1:02 PM (EST)
Well, I am running Excel version 10 (Office XP), and it is definitely possible to run more than one instance. 

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 1:02 PM (EST)
Well, I am running Excel version 10 (Office XP), and it is definitely possible to run more than one instance. 

     

davin.church
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 1:12 PM (EST)

How are you detecting that situation?  Excel has always been able to open more than one MDI window (each with its own list of spreadsheets), but I think it's always been sharing the base code/application object and just opening extra windows on it.

 

If this is not the case, then how does the above trick work?  Creating a new sub-object that comes pre-attached to a running application seems to imply that all sub-object instances share the same application object (regardless of what it might or might not have done in the past).

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 2:37 PM (EST)

I detect multiple instances of Excel by looking at the Windows Task Manager list of processes. 

 

If you use the Windows (File) Explorer to find the file excel.exe and double-click it a couple of times, I believe you'll launch a couple of instances.  On the other hand, if you start Excel by double-clicking XLS files, that probably uses DDE to simply open MDI documents in one instance. 

 

Looking at your Windows taskbar is not reliable -- there's an Excel option to show "Windows in Taskbar", which can confuse the situation.

     

Rex.Swain
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 2:38 PM (EST)

I detect multiple instances of Excel by looking at the Windows Task Manager list of processes. 

 

If you use the Windows (File) Explorer to find the file excel.exe and double-click it a couple of times, I believe you'll launch a couple of instances.  On the other hand, if you start Excel by double-clicking XLS files, that probably uses DDE to simply open MDI documents in one instance. 

 

Looking at your Windows taskbar is not reliable -- there's an Excel option to show "Windows in Taskbar", which can confuse the situation.

     

Support
Connect to Already-Running Excel
Posted: Monday, August 16, 2004 5:42 PM (EST)

> Is there a way to get control of which instance we connect to?

Don't know if this is possible.  If a solution is found, it will be posted here.

APL2000 Support

     

Mario.Linares
Connect to Already-Running Excel
Posted: Tuesday, September 28, 2004 5:06 PM (EST)

Hi

 

First of all, I'd like to say that I'm rather new to APL, so if I say something really dumb, please bear me.

 

I've been reading about APL - Excel interaction, and what I've found has been really useful, but I want to know the way to Open an Excel file having the name and location, just for reading purposes.

 

It may seem simlpe, and I'm sure it is, but I've been able to Create new files and work with them, but havn't been able to open an old file.

 

Any help would be appreciated!

 

Thanx.

     

M.A.Gaveau
Connect to Already-Running Excel
Posted: Saturday, October 02, 2004 1:24 PM (EST)

I have an old version of Excel (97) but if I try the following  instructions

'ex' Œwi 'Create' 'Excel.Application'
'ex'Œwi'Workbooks>ex.wkbks'
'ex.wkbks'Œwi'XOpen' 'pathname\Excel filename'
'ex'Œwi'visible'1  

it opens the corresponding Excel file.

I hope it will work for you too.

 

Marc-André Gaveau

 

 

     

Mario.Linares
Connect to Already-Running Excel
Posted: Monday, October 04, 2004 1:24 PM (EST)

It sure helped me!!!!

And it also works with Excel(2003).

 

Thanx a lot.

     



APL2000 Official Web Site

The true "final frontier" is in the minds and the will of people.
-- Gen. Michael E. Ryan, U.S. Air Force Chief of Staff

APLDN Home   |    |  Events   |  Trainings   |  APL Books   |  APLDN Links   |    |  Discussion Groups   |    |  Downloads   |  Articles   |  Library   |  Learning Tools   |  APLDN User IO   |  APL2000.com   |