03-21-2012 01:16 PM
I am writing a application that opens an Excel spreadsheet and writes data to it. I base my application on the Excel2000dem project in the activex samples for CVI 2009.
When I run my application I always get the error -2147221021 Operation Unavailable until I run the Excel2000dem program. Once that program runs, my application works exactly like the excel2000dem program.
It seems like something is not getting activated until the Excel2000dem program runs, but I can't see any difference in the code segments.
Solved! Go to Solution.
03-22-2012 01:15 AM
There si obviously something missing in your project that causes this error. I canot give you the exact file names now as I don't have CVI installed on this machine, but:
Also: which instruction gives you this error?
03-22-2012 09:35 AM
Roberto,
I have the excel2000.fp loaded in the project with the Microsoft Exelc 9.0 Object Library. Program compiles and runs with out any errors, after running the excel2000dem.
I get the error on the first instruction attempting to talk to Excel, error = Excel_ActiveApp(NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
03-22-2012 10:04 AM - edited 03-22-2012 10:07 AM
Excel_ActiveApp tries to connect to an already running instance of the product: if no Excel is running you get that "unavailable" error. Apparently you run Excel demo app and exit it without closing Excel: from that moment on your own app can run correctly. If this is true, you should be able to run your app without errors if you start Excel firstly. See this KB article for reference.
You can get rid of this problem with this code:
error = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle); if (error == MK_E_UNAVAILABLE) { wasActive = 0; // Excel not active: try to launch it errChk (Excel_NewApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle)); }
In this code the error is filtered out and determines whether to launch a new instance of the product or not.
'wasActive' flag determines if shutting down Excel on program exit or not: if true I run the following code at program exit. You may skip that last option if you are not interested to it.
if (!wasActive) Excel_AppQuit (ExcelAppHandle, &ErrorInfo);
error = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
if (error == MK_E_UNAVAILABLE) {
wasActive = 0;
// Excel non attivo: prova a lanciarlo
errChk (Excel_NewApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle));
}
03-22-2012 10:43 AM
Roberto,
Thanks, I see what I did wrong.
Paul