LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Excel ActiveX

Solved!
Go to solution

I am having trouble reading values back from aN Excel spreadsheet using the ActiveX library.  When I write a double to a cell, I can see it in the spreadsheet but when I read it back, I don't get a double but a CAObjectHandle.

 

Writing the data

lError = Excel_RangeSetItem(eoRangeHandle,
&stErrorInformation,
CA_VariantInt(iRow + 1),
CA_VariantInt(2),
CA_VariantDouble(999.99));

 

Reading the data into a variant.

lError = Excel_RangeGetItem(eoRangeHandle,
&stErrorInformation,
CA_VariantInt(iRow + 1),
CA_VariantInt(2),
&vData);

 

Checking the type of variant.

Type = CA_VariantGetType(&vData);

 

This returns 116 (CAVT_OBJHANDLE)

 

I have ran the sample programs and Excel_RangeGetItem returns a double.

 

What is wrong I have tried to copy the code from the sample program.

 

 

Thanks for any help.

0 Kudos
Message 1 of 3
(131 Views)

I have little experience in reading data from Excel as I normally only save data to spreadsheets, but I see in the example that Excel_RangeGetItem returns a pointer to a Dispatch object, which is probably what you are finding. It seems you have to access an additional object pointed to by the dispatch and read value back from that one.

 

This is the relevant code from Excel2000dem exsample:

// Ask for the ith by jth value of the range which returns a dispatch to a new single cell range
error = Excel_RangeGetItem (ExcelRangeHandle, &ErrorInfo, CA_VariantInt (i+1), CA_VariantInt (j+1), &MyVariant);
if (error<0) goto Error;

// Get the DISPATCH pointer
error = CA_VariantGetDispatch (&MyVariant, &MyDispatch);
if (error<0) goto Error;

// Create a new Range Object from DISPATCH pointer
error = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
if (error<0) goto Error;

// Get the value of the Single Cell Range
error = Excel_GetProperty (ExcelSingleCellRangeHandle, &ErrorInfo, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
if (error<0) goto Error;

if (!CA_VariantHasDouble (&MyVariant))
{
    MessagePopup(APP_WARNING, "Values returned were not of type DOUBLE.");
    goto Error;
}    

error = CA_VariantGetDouble (&MyVariant, &d);
if (error<0) goto Error;

// Free Variant element
CA_VariantClear(&MyVariant);

//Free Range Handle
ClearObjHandle (&ExcelSingleCellRangeHandle);

 

HTH, I'm afraid I cannot offer more than this.



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 3
(106 Views)
Solution
Accepted by topic author paul.knight@zeroavia.com

Thanks that was the problem.  There was a Dispatch object in between. 

0 Kudos
Message 3 of 3
(98 Views)