LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

open and read a excell file

Hi

I'm trying to open an existing excell file but the function Excel_WorkbooksOpen() returns error.

The code is as below :

 

err=Excel_NewApp(NULL,1,LOCALE_NEUTRAL,0,&applicationHandle);
 
 err=Excel_GetProperty(applicationHandle, NULL, Excel_AppWorkbooks,
                    CAVT_OBJHANDLE,&workbooksHandle);
                   
 err=Excel_WorkbooksOpen(workbooksHandle,NULL,"MyFile.xls",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, &workbookHandle);

 

A second question is :

How can I deterime how many rows the excell sheet has ?

Regards Per

0 Kudos
Message 1 of 7
(4,126 Views)

It looks like you are playing with the example givin by NI? \CVI\samples\activex\excel\excel2000dem.cws ? What do you mean by returns an error? Does it not compile? or does it compile but crash and return an error when you run it? Are you sure that the file you are trying to open exists and you are pointing to the proper location that the file is kept in? If it is in your main project folder and you are running the program in debug mode than the program will be running in a different folder than the xls is kept in. With a little more detail I'm sure we can get it working for ya.

 

Cheers

Douglas Sappet
Test Hardware Development Engineer
Allegro Microsystems Inc.
Manchester, NH
0 Kudos
Message 2 of 7
(4,121 Views)
Also I forgot to check and make sure that you have an instrument file for excel (an .fp file) included in your project, this is important. if you don't have one you can look at root\ . .... \National Instruments\CVI85\samples\activex\excel\excel2000.fp
Douglas Sappet
Test Hardware Development Engineer
Allegro Microsystems Inc.
Manchester, NH
0 Kudos
Message 3 of 7
(4,119 Views)

You are right, I'v copied lines from the excell2000 example into my app. The xls file is present in the same

directory as the source code.

 

My app compiles and do not crash, but the function Excel_WorkbooksOpen() returns the error code -2147352567

and the variable "workbookHandle" remains 0.

I do run the app in the debuger.

 

excelreport.fp is included in the project.

Regards Per

 

0 Kudos
Message 4 of 7
(4,106 Views)

Hi! Per,

 

You need to add two backslashes to your file name like this "\\MyFile.xls"

 

 

 

 

 

0 Kudos
Message 5 of 7
(4,006 Views)

If I'm not wrong you must pass a complete filename to Excel_WorkbooksOpen. Try this:

 

char   file[MAX_PATHNAME_LEN];

 

GetProjectDir (file);

MakePathname (file, "MyFile.xls", file);

error = Excel_WorkbooksOpen (ExcelWorkbooksHandle, NULL, file, 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);
 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 7
(3,995 Views)
It is obviously best practice to use a complete filename including the total path. But I tested out the example project and played around, finding that you can open things locally, so if define just myfile.xls it will open it as long as it is in the directory you are running the program out of, this means however if you are playing in debug mode, you need to put the file in the debug folder!
Douglas Sappet
Test Hardware Development Engineer
Allegro Microsystems Inc.
Manchester, NH
0 Kudos
Message 7 of 7
(3,988 Views)