views:

253

answers:

1

Using C++ and OLE, how might I go about obtaining the ID of the worksheet that is currently in focus?

For example, I have the following code:

    Variant excelSheets;
    Variant excelSheet;

    excelSheets.OleProcedure("Add");
    excelSheet= excelSheets.OlePropertyGet("Item", 1);

I would like to add a sheet and then get the sheet that was just added so that I may add content. The above code only works if the user doesn't shift focus away from the sheet which is at the far left.

Seth

A: 

I ended up using OlePropertyGet( "ActiveSheet" ); because when you add a sheet it becomes the ActiveSheet and you can work with it from there. I put an example of what I did below:

    Variant excelApp;
    Variant excelBooks; 
    Variant excelWorkBook; 
    Variant excelSheet; 
    Variant excelSheets;

 try
 {
  mExcelApp = Variant::GetActiveObject("Excel.Application");
 }
 catch(EOleSysError& e)
    {
        mExcelApp = Variant::CreateObject("Excel.Application"); //open excel
    }
    catch(...)
    {
        throw;
    }

    mExcelApp.OlePropertySet("ScreenUpdating", true);
    excelBooks = mExcelApp.OlePropertyGet("Workbooks");
    excelWorkBook = excelBooks.OlePropertyGet("Item",1);

    // a worksheet is added which becomes the active sheet
    excelSheets.OleProcedure( "Add" );
    excelSheet = excelWorkBook.OlePropertyGet( "ActiveSheet" );
Seth