none
Creating embedded charts in excel with Qt activex objects RRS feed

  • Question

  • I've tried to create embedded charts in excel with Qt activex objects and wrote some codes:

        QAxObject *excel = new QAxObject("Excel.Application", 0);
        QAxObject *workbook = excel->querySubObject("Workbooks")->querySubObject("Add");
        QAxObject* sheets = workbook->querySubObject( "Worksheets" );
        QAxObject* sheet1 = workbook->querySubObject("Worksheets(1)");
        QAxObject* charts = sheet1->querySubObject("ChartObjects");
        QList<QVariant> f; f << 200 << 400 << 200 << 200;
        QVariant ch = charts->dynamicCall("Add(int, int, int, int)", f );

    But the code gives following error msg:

    QAxBase: Error calling IDispatch member ChartObjects: Member not found

    What is the problem? It seems that the sheet is not valid but I am able to write into it!

    Wednesday, November 2, 2016 7:33 PM

All replies

  • Hi MostafaAlizadeh,

    Did you get error on this line “QAxObject* charts = sheet1->querySubObject("ChartObjects");”? Could you check whether sheet1 is null? As far as I know, for a new workbook, there is one worksheet named “Sheet1” instead of “Worksheets(1)”.

    I suggest you try something like below:

    QAxObject* sheet1 = workbook->querySubObject("Sheet1");

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 3, 2016 5:49 AM
  • Yes, The error occurs exactly in that line “QAxObject* charts = sheet1->querySubObject("ChartObjects");” and the sheet1 object is not null, I can even set cell values with this object.

    And for your suggestion, It gives following error:

    QAxBase::dynamicCallHelper: Sheet1: No such property in [unknown]

    Edit:

    Following codes works:

          
    QAxObject* excel = new QAxObject( "Excel.Application", 0 );
        QAxObject* workbooks = excel->querySubObject( "Workbooks" );
        QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", "D:\\test.xls" );
        QAxObject* sheets = workbook->querySubObject( "Worksheets" );
        QAxObject* sheet = sheets->querySubObject( "Item( int )", 1 );
        QAxObject* chart = sheet->querySubObject("ChartObjects(int)",1)->querySubObject("Chart");
    As seen, ChartObjects(int) has a member of Charts and the code works. So it seems that ChartObjects is not defined by itself or something like that.

    Thursday, November 3, 2016 5:42 PM
  • Hi MostafaAlizadeh,

    Thanks for sharing the solution.

    I would suggest you mark your reply as answer, and then others who run into the same issue would find the solution easily.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 2:49 AM
  • Hi Edward, Actually it's not solved! I'm still don't understand (as stated in previous reply) why the ChartObjects is not recognized. My previous reply state that ChartObjects(int) is a member; while in MSDN docs, the ChartObjects itself has Add method and is a member of sheet. I still can't add embedded charts to ChartObjects
    Friday, November 4, 2016 5:38 AM
  • Hi MostafaAlizadeh,

    Based on researching, I suggest you try something like below:

    //way1
    QAxObject *chart = workbook->querySubObject("Charts")->querySubObject("Add");
    //way2
    QAxObject* test = workbook->querySubObject("ActiveSheet");
     QAxObject* charts = test->querySubObject("ChartObjects");
     QList<QVariant> f; f << 200 << 400 << 200 << 200;
     QVariant ch = charts->dynamicCall("Add(int, int, int, int)", f );

    If it did not work, I would suggest you got to QT Forum for help, they are more familiar with it. This forum is developed with VBA or C#/VB.net, we are not familiar with QAxObject and might not be able to provide any further help for QAxobject.

    Sorry for any inconvenience.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 4, 2016 7:56 AM