To use the mechanism of interaction with other programs through the OLE (Windows) mechanism, it is convenient to use the ts_idispatcher object and the function of the same name.

This allows you to create your own functions and manipulate various Windows programs. For example, if there are not enough functions available in LABPP_Automat to work with Excel, you can create your own procedures.


Example 1

Connect to the currently active Excel spreadsheet to read the value from the current cell.

Display the resulting value in the program message window.


//------------------------------------------------------

// Example

// Read number current cell in Excell table

// LABPP 2022

//------------------------------------------------------

int main()

{

    int iIDispatchExcel;

    object("create","ts_idispatch",iIDispatchExcel);

    ts_idispatch(iIDispatchExcel,"attach","Excel.Application");  // create dispatcher object 


    int iVariantRange;

    object("create","ts_variant",iVariantRange);   // create variant object to obtain dispatcher object of the current active cell

    int ires = ts_idispatch(iIDispatchExcel, "AutoWrap", iVariantRange, "DISPATCH_PROPERTYGET", "ActiveCell", 0);


    if(ires != 0) {

          return -1;

    }


    int iIDispatchActiveCell;

    object("create","ts_idispatch",iIDispatchActiveCell);

    ts_variant(iVariantRange,"get_pdispVal", iIDispatchActiveCell);


    int iVariantParm;

    object("create","ts_variant",iVariantParm);

//    ts_variant(iVariantParm,"set_dblVal",111.10);

//    ts_variant(iVariantParm,"set_bstrVal","Stop War");

 

    ires = ts_idispatch(iIDispatchActiveCell,"AutoWrap", iVariantParm,"DISPATCH_PROPERTYGET", "FormulaR1C1", 0);

//    ires = ts_idispatch(iIDispatchActiveCell,"AutoWrap", iVariantParm,"DISPATCH_PROPERTYGET", "Value", 0);


    if (ires == 0)

    {

        string s;

        ts_variant(iVariantParm, "get_value_simple",s);

        coutvar << s;

        int i;

        ts_variant(iVariantParm, "get_value_simple",i);

        coutvar << i;

        double d;

        ts_variant(iVariantParm, "get_value_simple",d);

        coutvar << d;

    }

    object("delete",iVariantParm);

    object("delete",iVariantRange);

    object("delete",iIDispatchActiveCell);


    ts_idispatch(iIDispatchExcel,"detach");

    object("delete",iIDispatchExcel);

}


Example 2

Connect to the currently active Excel spreadsheet to write the value to the current cell.


//------------------------------------------------------

// Example

// Write number 111.10 to current cell in Excell table

// LABPP 2022

//------------------------------------------------------

int main()

{

    int iIDispatchExcel;

    object("create","ts_idispatch",iIDispatchExcel);

    ts_idispatch(iIDispatchExcel,"attach","Excel.Application");  // create dispatcher object 


    int iVariantRange;

    object("create","ts_variant",iVariantRange);   // create variant object to obtain dispatcher object of the current active cell

    int ires = ts_idispatch(iIDispatchExcel, "AutoWrap", iVariantRange, "DISPATCH_PROPERTYGET", "ActiveCell", 0);


    if(ires != 0) {

          return -1;

    }


    int iIDispatchActiveCell;

    object("create","ts_idispatch",iIDispatchActiveCell);

    ts_variant(iVariantRange,"get_pdispVal", iIDispatchActiveCell);


    int iVariantParm;

    object("create","ts_variant",iVariantParm);

//    ts_variant(iVariantParm,"set_dblVal",111.10);

    ts_variant(iVariantParm,"set_value_simple","Stop War");

 

    int iVariantNull;

    object("create","ts_variant",iVariantNull);


    ires = ts_idispatch(iIDispatchActiveCell,"AutoWrap", iVariantNull,"DISPATCH_PROPERTYPUT", "FormulaR1C1", 1, iVariantParm);


    object("delete",iVariantParm);

    object("delete",iVariantRange);

    object("delete",iIDispatchActiveCell);

    object("delete",iVariantNull);


    ts_idispatch(iIDispatchExcel,"detach");

    object("delete",iIDispatchExcel);

}