Thursday, April 7, 2011

Excel automation. How to copy more than one cell?

For instance:

 //omitted

vector<_bstr_t> cellData;

Excel::_WorksheetPtr pSheet = application->ActiveSheet;

Excel::RangePtr pRange = application->Cells;

_bstr_t cellValue = pRange->Item[1][1]; //single cell

cellData.push_back(cellValue);

 //omitted

Without:

  • MFC
  • ATL

Question:

  • How to copy, multiple cells, for instance A1:B1, into the vector?
  • Is there a more appropriate method, instead of Item?
From stackoverflow
  • I've never done any Excel automation using C++, but if it works similarly as VBA macros, the WorkSheet class should have a Range property that returns a range of cells in the sheet. You can specify what range you want either by giving two cells id, or a string representing the range.

    Code in pseudo VBA:

    Dim range As Range
    Set range = ActiveSheet.Range("B12:D34")
    Set range = ActiveSheet.Range("B12", "D34")
    Set range = ActiveSheet.Range("B12", ActivSheet.Range("D34"))
    ...
    

    See MSDN for more information about the Range class.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.