Excel automation, cell reading problem

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
In need of some credit
In need of some credit
Posts: 1
Joined: Sun Jul 20, 2008 2:20 pm

Excel automation, cell reading problem

Post by titux » Mon Aug 04, 2008 9:20 am

Hi everybody,

I'm looking for a way of reading an excel file via the wxWidgets
automation. In a first time I try to read the content of a cell
which is not the active cell.
For this one it's not a problem, but reading an other cell is impossible for me
now because I don't how to do in despite
of the hours I spend to search on the web and in the wxWidgets doco.

At the moment, I try with the following code :

Code: Select all

wxAutomationObject xlbook, cell;
    wxVariant appo, rng[4], new_name;
    bool    res;
    objExcel = new wxAutomationObject;
    if ( objExcel->CreateInstance("Excel.Application") ){
        wxVariant contenu(1.1);
        rng[0] = wxVariant(fichier);
        res = objExcel->GetObject(xlbook, "Workbooks.Open", 1, rng);
        appo=objExcel->PutProperty("visible", true);
        cout << " data reading " << endl;
        wxVariant result = objExcel->GetProperty("ActiveCell.Value");
        wxString value = result.GetString();
        cout << "value " << value << endl;
        res = objExcel->PutProperty("Cells.Activate", "A2");
        result = objExcel->GetProperty("ActiveCell.Value");
        value = result.GetString();
        cout << "value " << value << endl;
the first reading, the one of the active cell, works contrary to the A2's reading.

So if someone knows a way to read a cell which is not the active cell,
or a way to make active the cell I want, I'm interested.

I'm also interested by any information about wxWidgets automation
because it's not a very documented functionnality, I think.

Thank you everybody by advance.
See you soon.


Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg » Mon Aug 11, 2008 7:49 pm

Why not try to get the cell object, then read it's value directly instead of activating it. Like this:

Code: Select all

        wxAutomationObject cell;
        wxVariant rng[2];
        int row, col;
        row = 1;
        col = 1;

        rng[0] = wxVariant(row);
        rng[1] = wxVariant(col);

        excelObject.GetObject(cell, "Cells", 2, rng);

Post Reply