LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how to open excel workbook in labwindows/cvi

I'm trying to open Excel, open an existing Excel file (workbook) and extract data from the first worksheet. I have successfully opened Excel, but am having
trouble opening the workbook. To open the workbook, I am trying to use the library function (from MS Excel 10.0 Object Library) Excel_OpenWorkbook.

However, when I attempt to use this function during a callback, nothing happens and the Excel file does not open. I get a status message of -2147221164 or
"Class not registered".

I looked at the sample project, excel2000dem.prj, but that project uses functions from the 9.0 and 8.0 Excel Object Library and isn't too helpful.

I have included the code I used to launch Excel and the code I used to open a new workboo
k.
"noiseParamWorkhandle" is the handle I assign to the new workbook. Please help me or point me toward the right resource. Thanks.
0 Kudos
Message 1 of 3
(4,340 Views)
This isn't the way ActiveX programming works. The workbook has to opened INSIDE the context of the application object that you created with Excel_NewApp. In ActiveX, this relationship is managed through collections. The Application object (that you created with Excel_NewApp) has a collection of workbooks. To open a workbook in that application, you have to get the collection of workbooks from the application object and then open the workbook inside that collection. You can see how this is done in the example you referred to in the OpenAppFile function. First, it gets the application object's workbooks collection with:

error = Excel_GetProperty (ExcelAppHandle, NULL, Excel_AppWorkbooks, CAVT_OBJHANDLE, &ExcelWorkbooksHandle);

Then, it opens a workboo
k from a file and adds it to that collection with:

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);

Hopefully that makes sense. It takes some time to get used to ActiveX programming and the object relationships like collections. The example you referred to is a good reference for the excel objects.

Best Regards,

Chris Matthews
National Instruments
0 Kudos
Message 2 of 3
(4,340 Views)
hi,

I've been also kind of greenhorn in ActiveX programming two months ago. Now I'm a little bit more experienced and I use ExcelXP. So I had the same problems as you have.

First of all you should create a *.fp for Excel 10.0 library. You can do this via the ActiveX controller wizard. The names of the functions and the number of arguments will differ but the algorithms stay the same. Perhaps it's useful if you try to understand the CVI samples and transfer the function calls to the proper one's for ExcelXP. This will take some time but it helps to understand the basic principles of ActiveX and object oriented programming. By the way: CVI accesses the VBAXL10.CHM helpfile from Excel for function explanation. The samples (because they're based on former librarie
s refer to VBAXL8.CHM or VBAXL9.CHM. So it could be helpful if you install CVI also on a PC where Excel2000 is installed. So you can better see the changes.

Now one question from me:
I still have problems using the RangeSort command within my code. For me it looks like a bug in the library because I use the right handle and the function arguments are also the right ones. NI is not very cooperative in those 3rd party problems,they prefer to refer to Microsoft.

Tell me, if I could have helped you (or if you can help me 🐵 )!
0 Kudos
Message 3 of 3
(4,340 Views)