LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

-2147221021 Operation Unavailable

Solved!
Go to solution

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.

0 Kudos
Message 1 of 5
(4,389 Views)

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:

 

  • Have you included all relevant include files in the source files?
  • Have you loaded the Excel interface instrument?
  • Have you added it to the project?

Also: which instruction gives you this error?



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 2 of 5
(4,381 Views)

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

0 Kudos
Message 3 of 5
(4,374 Views)
Solution
Accepted by topic author Paul_Knight_Lockheed_Mart

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



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 4 of 5
(4,368 Views)

Roberto,

 

Thanks, I see what I did wrong.

 

Paul

0 Kudos
Message 5 of 5
(4,360 Views)