none
Excel sheet selection with COM C++ RRS feed

  • Question

  • Hello,
    There is a great example for writing data to Excel with COM:

    https://support.microsoft.com/en-my/help/216686/how-to-automate-excel-from-c-without-using-mfc-or- import

    But the problem is that there the data is write in the active sheet, but i need a write not active, that is, i need to select a sheet for writing.

    But unfortunately I can not understand how to change the code for this.

    In theory, it is necessary, as it is, to replace the pXlSheet parameterwith one that is needed, but I cannot understand how to do it:

      // Get ActiveSheet object
       IDispatch *pXlSheet;
       {
          VARIANT result;
          VariantInit(&result);
          AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"ActiveSheet", 0);
          pXlSheet = result.pdispVal;
       }
    
       // Get Range object for the Range A1:O15...
       IDispatch *pXlRange;
       {
          VARIANT parm;
          parm.vt = VT_BSTR;
          parm.bstrVal = ::SysAllocString(L"A1:O15");
    
          VARIANT result;
          VariantInit(&result);
          AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
          VariantClear(&parm);
    
          pXlRange = result.pdispVal;
       }

    If not difficult, please tell me how this can be done.


    Monday, March 4, 2019 11:32 AM