ni.com is currently undergoing scheduled maintenance.
Some services may be unavailable at this time. Please contact us for help or try again later.
09-16-2015 05:20 AM
Hi,
I am trying to write data into a particular cell in an excel. when i am looking into example program i felt bit difficult to understand.
i have wriiten code to read data from particular cell which works well. I am facing difficulties in writing data to cell. pls someone help.
// to read a data from cell which works
status = Excel_RangeGetItem (rangeHandle, NULL, CA_VariantInt (i+1), CA_VariantInt (j+1), &MyVariant);
// Get the DISPATCH pointer
status = CA_VariantGetDispatch (&MyVariant, &MyDispatch);
// Create a new Range Object from DISPATCH pointer
status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
// Get the value of the Single Cell Range
status = Excel_GetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
status= CA_VariantGetCString(&MyVariant,&val);
strcpy(cellvalue,val);
// to write data into a cell
status = Excel_RangeSetItem (rangeHandle, NULL, CA_VariantInt (i+1), CA_VariantInt (j+1), MyVariant);
status = CA_VariantSetDispatch (&MyVariant, &MyDispatch);
status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
status = Excel_SetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
status= CA_VariantSetCString(&MyVariant,writedata);
i know i am doing some mistake in the above code.
Solved! Go to Solution.
09-17-2015 05:20 AM
Can someone help. i need to set a value in the ith row and jth column in an excel as i did for reading cell value.
void write_cell_value(int i, int j, char *cellvalue)
{
static VARIANT MyVariant;
status= CA_VariantSetCString(&MyVariant,cellvalue);
status = Excel_RangeSetItem (rangeHandle, NULL, CA_VariantInt (i), CA_VariantInt (j), MyVariant);
}
09-17-2015 05:59 AM
As far as I can see your write_cell_Value function only needs to receive 'rangeHandle' parameter, then it should work correctly. Also cut the static keyword from the definition of MyVariant and free it at the end with CA_VariantClear.
Do you receive any error when running the program?
09-17-2015 06:41 AM
I removed static keyword and it works. thank you very much Roberto.
i am facing another probelm in reading cell value when my cell is kept empty.
My cellrange is A1:C6 in which A3 and A4 cells doesnt have any data.
i am getting Function CA_VariantGetCString: (return value == -2147352571 [0x80020005]). Type mismatch error when trying to read any cell with empty.
void read_cell_value(int i,int j, char *cellvalue)
{
static VARIANT *ValueVPtr,*Varray;
static LPDISPATCH MyDispatch;
char *val;
status = Excel_RangeGetItem (rangeHandle, NULL, CA_VariantInt (i), CA_VariantInt (j), &MyVariant);
status = CA_VariantGetDispatch (&MyVariant, &MyDispatch);
status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
status = Excel_GetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
status= CA_VariantGetCString(&MyVariant,&val);
strcpy(cellvalue,val);
}
09-17-2015 08:30 AM
CA_VariantIsEmpty function can be of help in this respect: if not empty then read the value.
09-17-2015 11:19 PM
Thank you very much Roberto. You have solved my issue.