範囲指定したCellのコピーと貼り付け



//Excelを操作するためのタイプライブラリを読みこむ(Excel2002用)
#import "C:\Program Files\Common Files\Microsoft Shared\Office10\MSO.DLL" no_namespace rename("DocumentProperties", "DocumentPropertiesXL")   
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\Vbe6ext.olb" no_namespace   
#import "C:\Program Files\Microsoft Office\Office10\EXCEL.EXE" rename("DialogBox", "DialogBoxXL") rename("RGB", "RGBXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces


void CXlsCellCpyPastDlg::OnButton1() 
{
    int i;
    char buffer[9];
    COleVariant data,data1;
    using namespace Excel;
    _ApplicationPtr pXL;

     //Excelの起動
    pXL.CreateInstance(L"Excel.Application");
    pXL->Visible = TRUE;

    //WorkBookを追加する
    WorkbooksPtr pBooks = pXL->Workbooks;
    _WorkbookPtr pBook  = pBooks->Add();

    //アクティブ・シートを取得
    _WorksheetPtr pSheet = pXL->ActiveSheet;

    //Cellsを使用したデータの出力
    for(i = 1; i < 11; i++){
        data = (short)i;
        data1 = (short)1;
        RangePtr pCells = pSheet->Cells->Item[data][data1];
        itoa(i,buffer,10);
        pCells->Value2 = buffer;
    }

    //------------------------------------------------------
    //シート内のCellをクリップ・ボードへコピー
    RangePtr pRange = pSheet->Range[COleVariant(pSheet->Cells->Item[COleVariant((short)1)][COleVariant((short)1)])]
                       [COleVariant(pSheet->Cells->Item[COleVariant((short)10)][COleVariant((short)1)])];
    pRange->Copy(vtMissing);

    //アクティブ・シートを移動
    data = (short)2;
    pSheet = pBook->Worksheets->Item[data];
    pSheet->Select();

    //------------------------------------------------------
    //クリップ・ボードへコピーしたデータを貼り付ける 
    RangePtr pRange1 = pSheet->Cells->Item[COleVariant((short)1)][COleVariant((short)1)];
    pRange1->PasteSpecial(xlPasteAll, xlPasteSpecialOperationNone);

    AfxMessageBox("動作確認のために一時停止");

    pXL->DisplayAlerts = false;
    pXL->Quit();
}


[サンプル・ソース]