[SOLVED] Reading MS Word in Qt, please!



  • Hey there, I am both new to Qt and do not have much experience in C++. I need to make program which opens .docx file and gets text and table contents from it to process it later; I managed to show document contents in TextEdit widget though. I looked through quite a lot of posts but still don't get it. I both open document and display it in a widget with this piece of code:
    @
    QAxWidget* WordDoc=new QAxWidget ("Word.Document", this-> ui-> textEdit);
    WordDoc->setGeometry (QRect (10, 10, 761, 511));
    WordDoc->setControl (filename.join(""));
    WordDoc->show();
    @

    , where "filename" is QStringList variable. I figured out that I need to use querySubObject() and dynamicCall() functions which take COM object's specific methods as parameters, but I can't understand how to find a full list of MS Word's - compatible methods. I've tried to generate documentation by WordDoc->generateDocumentation() function which gave me list of some signals, slots, parameters, functions etc, but I did not seem to find anything I needed. So can anyone explain how do I use querySubObject and preferably some code example to get hang of it? Thanks in advance!



  • first try to implement his plan in more user-friendly for a Word environment such vba You can test the methods through ActiveX / COM Inspector



  • [quote author="rst256" date="1409328817"]first try to implement his plan in more user-friendly for a Word environment such vba You can test the methods through ActiveX / COM Inspector[/quote]
    Thanks, and I finally managed to get things work.
    Here's some info to help people like me to understand a few basic things:

    querySubObject() function is used to navigate through MS Word's or other COM object's object model tree. Such tree for MS Word can be found here: http://msdn.microsoft.com/en-us/library/aa220474(v=office.10).aspx
    dynamicCall() function is used to call object's specific methods. Documentation for specific object type can be generated via code
    @ui->textEdit->setText(object_name->generateDocumentation());@
    Details for every method and whatnot can be found in Microsofts VBA documentation: http://www.microsoft.com/en-us/download/details.aspx?id=40326
    Note that for every sub object different documentation is generated.

    And here's part of code which opens up .docx document that contains text only and prints its contents to textEdit:

    @QAxObject wordApplication = new QAxObject("Word.Application", this);
    QAxObject documents = wordApplication->querySubObject("Documents");
    QAxObject
    document = documents->querySubObject("Open(const QString&, bool)", "C:\Blah\Blah\Blah\Microsoft Word.docx", true);
    QAxObject
    words = document->querySubObject("Words");
    QString textResult;
    int countWord = words->dynamicCall("Count()").toInt();
    for (int a = 1; a <= countWord; a++){
    textResult.append(words->querySubObject("Item(int)", a)->dynamicCall("Text()").toString());
    }
    ui->textEdit->setText(textResult);@

    Now, I hope, you got the basics of working with COM objects. Good luck!


  • Lifetime Qt Champion

    Nice !

    Thanks for sharing your newly acquired knowledge ! :)

    Can you also update the thread title prepending [solved] ? So other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.