LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

copying formule from one cell to other cell using cvi

hi,

how to copy formule from one cell to the other cell in the excel sheet using cvi functions.

 

Thanks & Regards

0 Kudos
Message 1 of 2
(2,916 Views)

First, I assume you have studied the example CVI project "excel2000dem.prj" that comes with the installation of your LabWindows/CVI.  From this example, you know how the function "ReadDataFromExcel(...)" function in excel2000dem.c works. 

 

Now, suppose you have a formula "=D3+E4" in cell "D35" and you want to copy this formula to cell "Z9".  Here's how you do it.

 

 

//-----------------------------------------------------------------------------
// 1) Get a formula from a cell D35.  
//-----------------------------------------------------------------------------
    
    // Open and activate one cell only
    error = CA_VariantSetCString (&MyCellRangeV, "D35");
    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, &ExcelSingleCellRangeHandle);
    if (error<0) goto Error;

   

    // Make range Active
    error = Excel_RangeActivate (ExcelSingleCellRangeHandle, &ErrorInfo, NULL);
    if (error<0) goto Error;

   

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

    printf("%s\n ", str);          //<--- the formula in now stored in the character array "str"


   Error:
    CA_VariantClear(&MyVariant);
    CA_VariantClear(&MyCellRangeV);

    ClearObjHandle (&ExcelSingleCellRangeHandle);

    if (error < 0)
        ReportAppAutomationError (error);

    return error;

 

 

 

//-----------------------------------------------------------------------------
// 2) Write a formula to a cell Z9.  
//-----------------------------------------------------------------------------
 

    // Open and activate one cell only
    error = CA_VariantSetCString (&MyCellRangeV, "Z9");
    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, &ExcelRangeHandle);
    if (error<0) goto Error;

   

    // Make range Active
    error = Excel_RangeActivate (ExcelRangeHandle, &ErrorInfo, NULL);
    if (error<0) goto Error;

   

    error = Excel_SetProperty (ExcelRangeHandle, &ErrorInfo,
                               Excel_RangeValue2, CAVT_CSTRING, str);

 

   Error:

    CA_VariantClear(&MyCellRangeV);
    ClearObjHandle (&ExcelRangeHandle);

   

    if (error < 0)
        ReportAppAutomationError (error);

 

 

/***************************************************************************************************/

 

 

Again, you have to study the example code in excel2000dem.c to educate yourself about how CVI works with Excel.

0 Kudos
Message 2 of 2
(2,885 Views)