11-11-2024 04:19 PM
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.
Solved! Go to Solution.
11-12-2024 05:55 AM
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.
11-12-2024 06:30 AM
Thanks that was the problem. There was a Dispatch object in between.