Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Reading MS Word in Qt, please!

[SOLVED] Reading MS Word in Qt, please!

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 6.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karakatiza
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rst256
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Karakatiza
        wrote on last edited by
        #3

        [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!

        1 Reply Last reply
        3
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • AlbatorA Offline
            AlbatorA Offline
            Albator
            wrote on last edited by
            #5

            Tips :
            For those who try to reuse this code do not forget to put in the .pro QT += axcontainer gui widgets

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved