LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to connect to an already open excel document.

From looking thru examples do not readily understand how to connect to an excel document that is ALREADY opened, thus not already opened by cvi thru activex.

Basically have an application that opens excel and opens the excel document that is not opened by CVI(it is opened via other means). I then need to establish connection to this already opened document using cvi to read data only, using cvi object library
commands as usual.

cvier
0 Kudos
Message 1 of 9
(4,479 Views)
Hello cvier,

In order to connect to an already open instance of Excel instead of opening a new one, use the function Excel_ActiveApp (...) instead of Excel_NewApp (...). This ActiveApp function will return a handle to the Excel application that can be used further on in your code.

Once you have a handle to the already opened instance of Excel, you can get a handle to an already opened WorkBook by using the Excel_ActiveWorkbook (...) instead of the Excel_WorkbooksOpen (...). Likewise, this function will give you a handle to the already open workbook and you will be able to modify the contents of the workbook, etc. through this handle.

I would suggest taking a look at the excel shipping example (National Instruments\CVIxx\samples\activex\excel\excel2000dem) and modify the OpenAppFile callback to use the ActiveWorkbook function instead of the WorkbooksOpen function. Run the application, and "Connect" to Excel instead of "Open" Excel. Select "Open File" and then select "Write Data". You will notice that the data is now added to the workbook that was already open in Excel.

I hope that works out. Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 9
(4,462 Views)
Tried as mentioned. Basically have excel opened with the worksheet visible by means
of me opening it manually.

Changed out WorkbooksOpen function in OpenAppFile routine for ActiveWorkbook function.
When I use the demo and use connect function, I see another Excel window(app) open up with the work book showing. Upon running the OpenAppFile routine the ActiveWorkbook
routine gives an error of "operation unavailable". Is there something I am missing?

This is the ActiveWorkbook code line using.

error = Excel_ActiveWorkbook(NULL,0,LOCALE_NEUTRAL,0,&ExcelWorkbookHandle);

cvier
0 Kudos
Message 3 of 9
(4,458 Views)
Hello cvier,

Let me step through my process and setup.

I am working with Excel 2002 SP 3, Win XP, and CVI 7.1.

I opened the project I mentioned above, and edited the OpenAppFile routine. I commented out the function Excel_WorkbooksOpen and inserted a call to Excel_ActiveWorkbook (NULL, 0, LOCALE_NEUTRAL, 0, &ExcelWorkbookHandle);

I then built and ran the application. On the UIR, I selected, "Connect" then "Open File" (which didn't open a new instance of Excel or a new instance of my workbook). Then I selected "Write Data", and the generated data was added to my excel file.

What is the setup you are currently using?
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 4 of 9
(4,445 Views)
Wendy, tried it again, to make sure it is exactly as described. I see the same response.
It opens another instance when "connect to excel' is selected, then get "operation unavailable" error for the error response from the ActiveWorkbook function in
the openAppFile functiion.

I am using CVI5.51 with Win2000 SP3 with Excel 9.0 object cvi library.
I can get it to work very reliably in its original state of launching excel..etc.

cvier
0 Kudos
Message 5 of 9
(4,443 Views)
Which version of Excel are you using? Have you tried this on another machine? Do you see the same behavior?

Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 6 of 9
(4,438 Views)
Wendy, good point. I will try it on another machine to see.
It is excel version 9.0.3821 SR-1.

cvier.
0 Kudos
Message 7 of 9
(4,435 Views)
Tried it with CVI 6.0, Excel 9.0 objects (office 2000) and win2000 on another machine.
Get same response and behavior. I must be doing something differently than you.
cvier
0 Kudos
Message 8 of 9
(4,404 Views)

Hello,

I just tested the behavior on a comparable machine and setup and was unable to reproduce the behavior.

The only edit I made was to the Open function. My edits are shown below:

/*error = Excel_WorkbooksOpen (ExcelWorkbooksHandle, NULL, fileName, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, &ExcelWorkbookHandle);
*/
Excel_ActiveWorkbook (NULL, 0, LOCALE_NEUTRAL, 0, &ExcelWorkbookHandle);


I open my Excel workbook and make my worksheet active. After running the CVI application, I select "Connect", "Open" then "Write Data" on the front panel. Another Excel document is not opened on all the test machines I have used.


The only difference software-wise with my setup is that I have the most recent version of the CVI runtime engine. The CVI runtime engine is backwards compatible, so you will be able to use the newest version with CVI 6.0. I would suggest downloading the latest version and trying it out.


Thanks.

Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 9 of 9
(4,385 Views)