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. How to read and write docx files in Qt
Forum Updated to NodeBB v4.3 + New Features

How to read and write docx files in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 9 Posters 26.9k Views 3 Watching
  • 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.
  • D Offline
    D Offline
    Dante Pham
    wrote on last edited by
    #21

    Ok so thanks to @VRonin , I found this https://youtu.be/qOfiiYP2JTI?t=2m11s , in which is pretty much what I'm looking for ! The only problem is I don't know C# . Is there any way we can do that in Qt in C++ ??

    mrjjM VRoninV 2 Replies Last reply
    0
    • D Dante Pham

      Ok so thanks to @VRonin , I found this https://youtu.be/qOfiiYP2JTI?t=2m11s , in which is pretty much what I'm looking for ! The only problem is I don't know C# . Is there any way we can do that in Qt in C++ ??

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #22

      @Dante-Pham
      Well if you know c++ then C# is pretty easy.
      https://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx

      I think you can do it almost same way with Visual Studio and c++ but of course the syntax will be
      different.

      D 1 Reply Last reply
      0
      • mrjjM mrjj

        @Dante-Pham
        Well if you know c++ then C# is pretty easy.
        https://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx

        I think you can do it almost same way with Visual Studio and c++ but of course the syntax will be
        different.

        D Offline
        D Offline
        Dante Pham
        wrote on last edited by
        #23

        @mrjj Ok I got 5 more days to finish this ... And I have never used visual studio.

        mrjjM D 2 Replies Last reply
        0
        • D Dante Pham

          @mrjj Ok I got 5 more days to finish this ... And I have never used visual studio.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #24

          @Dante-Pham
          oh. that might be a bit tight then.
          Im not sure what he use in the video . maybe this ?
          http://www.aspose.com/api/net/words/aspose.words/documentbuilder

          1 Reply Last reply
          1
          • D Dante Pham

            Ok so thanks to @VRonin , I found this https://youtu.be/qOfiiYP2JTI?t=2m11s , in which is pretty much what I'm looking for ! The only problem is I don't know C# . Is there any way we can do that in Qt in C++ ??

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #25

            @Dante-Pham said in How to read and write docx files in Qt:

            Is there any way we can do that in Qt in C++ ??

            As mentioned above you'll need to use a dialec of C++ called C++/CLI.
            You can download visual studio from https://www.visualstudio.com/vs/community/ you can find a crash course in the dialect here: https://www.codeproject.com/articles/19354/quick-c-cli-learn-c-cli-in-less-than-minutes
            Given the name googling it is a nightmare you are better off just googling stuff in C# and either translate it (it's not that hard) or if you go on msdn the examples are often available in C#, VB.Net and more importantly C++/CLI (see for example https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp)

            @Dante-Pham said in How to read and write docx files in Qt:

            DocX only has a .DLL file and a documentation

            That's all you need, you use #using <DocX.dll> to include it in the source file (that needs to be compiled with the /clr option )

            Here you can find a small example of a qmake project using this paradigm

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • D Dante Pham

              @mrjj Ok I got 5 more days to finish this ... And I have never used visual studio.

              D Offline
              D Offline
              Devopia53
              wrote on last edited by Devopia53
              #26

              @Dante-Pham

              Why not use QAxObject for MS ActiveX?
              This is the solution you want to use like this:

              QString     outFile("C:/test.docx");
              QString     inFile1("C:/test1.docx");
              QString     inFile2("C:/test2.docx");
              QAxObject   axObject("Word.Application");
              QAxObject   *documents = axObject.querySubObject("Documents");
              QAxObject   *document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
              QAxObject   *selection = axObject.querySubObject("Selection");
              
              selection->dynamicCall("EndKey(QVariant&)", 6); // WdUnits::wdStory=6
              selection->dynamicCall("InsertBreak(QVariant&)", 7); // WdBreakType::wdPageBreak=7
              selection->dynamicCall("InsertFile(QString&)", inFile2);
              
              document->dynamicCall("SaveAs(const QString&)", outFile);
              document->dynamicCall("Close()");
              axObject.dynamicCall("Quit()");
              
              

              You need a validation check for all pointers!

              VRoninV D 2 Replies Last reply
              2
              • D Devopia53

                @Dante-Pham

                Why not use QAxObject for MS ActiveX?
                This is the solution you want to use like this:

                QString     outFile("C:/test.docx");
                QString     inFile1("C:/test1.docx");
                QString     inFile2("C:/test2.docx");
                QAxObject   axObject("Word.Application");
                QAxObject   *documents = axObject.querySubObject("Documents");
                QAxObject   *document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
                QAxObject   *selection = axObject.querySubObject("Selection");
                
                selection->dynamicCall("EndKey(QVariant&)", 6); // WdUnits::wdStory=6
                selection->dynamicCall("InsertBreak(QVariant&)", 7); // WdBreakType::wdPageBreak=7
                selection->dynamicCall("InsertFile(QString&)", inFile2);
                
                document->dynamicCall("SaveAs(const QString&)", outFile);
                document->dynamicCall("Close()");
                axObject.dynamicCall("Quit()");
                
                

                You need a validation check for all pointers!

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #27

                @Devopia53 said in How to read and write docx files in Qt:

                Why not use QAxObject for MS ActiveX

                The main point is that to use activeX you need MS Word installed, if you use an independent library you do not

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                mrjjM 1 Reply Last reply
                1
                • VRoninV VRonin

                  @Devopia53 said in How to read and write docx files in Qt:

                  Why not use QAxObject for MS ActiveX

                  The main point is that to use activeX you need MS Word installed, if you use an independent library you do not

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #28

                  Hi
                  In posters use case, there will always be word as the users
                  will create the input in that application.
                  However, @VRonin explanation on how to mix .NET with Qt in Visual studio does
                  sound awfully easy and would most likely be the most straightforward as DocX seems
                  very high level.
                  Time is short however and he never used VS before so might be slightly uphill to get going. :)

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    Dante Pham
                    wrote on last edited by
                    #29

                    Just to be clear, MS Word will always be installed along with this app ( if it's not already installed )
                    Currently I'm using QAxObject, however, I can't find the functions that i need (most importantly copying, pasting and adding customizable-style-strings) anywhere. Having this will probably solve the whole problem.
                    Using Visual Studio to me is very overwhelming. I lost quite a few times using it. I'm familiar with Dev C++ and CodeBlock and they are quite simple.

                    1 Reply Last reply
                    0
                    • D Devopia53

                      @Dante-Pham

                      Why not use QAxObject for MS ActiveX?
                      This is the solution you want to use like this:

                      QString     outFile("C:/test.docx");
                      QString     inFile1("C:/test1.docx");
                      QString     inFile2("C:/test2.docx");
                      QAxObject   axObject("Word.Application");
                      QAxObject   *documents = axObject.querySubObject("Documents");
                      QAxObject   *document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
                      QAxObject   *selection = axObject.querySubObject("Selection");
                      
                      selection->dynamicCall("EndKey(QVariant&)", 6); // WdUnits::wdStory=6
                      selection->dynamicCall("InsertBreak(QVariant&)", 7); // WdBreakType::wdPageBreak=7
                      selection->dynamicCall("InsertFile(QString&)", inFile2);
                      
                      document->dynamicCall("SaveAs(const QString&)", outFile);
                      document->dynamicCall("Close()");
                      axObject.dynamicCall("Quit()");
                      
                      

                      You need a validation check for all pointers!

                      D Offline
                      D Offline
                      Dante Pham
                      wrote on last edited by Dante Pham
                      #30

                      @Devopia53 I'm not sure what youre trying to implement there. I tested it out and the three files didnt change at all. If you know how to copy, paste from one docx to another, add strings using QAxWidget, that would be awesome

                      D 1 Reply Last reply
                      0
                      • D Dante Pham

                        @Devopia53 I'm not sure what youre trying to implement there. I tested it out and the three files didnt change at all. If you know how to copy, paste from one docx to another, add strings using QAxWidget, that would be awesome

                        D Offline
                        D Offline
                        Devopia53
                        wrote on last edited by
                        #31

                        @Dante-Pham

                        The example I provided is simply merging Document1(inFile1) and Document2(inFile2) to create Document3(outFile).

                        For more information, check out Microsoft's site.

                        1 Reply Last reply
                        1
                        • D Offline
                          D Offline
                          Dante Pham
                          wrote on last edited by
                          #32

                          One last thing
                          Let's say i have to create a .docx file that has 50 questions and it should look like this:
                          Header
                          Questions 1: { insert docx to this area }
                          Questions 2: { insert docx to this area }
                          ...
                          Questions 50: { insert docx to this area }
                          Footer
                          I implemented this code:

                              QAxObject   axObject("Word.Application");
                              QAxObject   *documents = axObject.querySubObject("Documents");
                              QAxObject   *document = documents->querySubObject("Open(const QString&, bool)", Header, true); 
                              QAxObject   *selection = axObject.querySubObject("Selection");
                              selection->dynamicCall("EndKey(QVarient&)", 6); // WdStoryType::wdStory=6
                          for(int i=1; i<=50; i++)
                          {
                              selection->dynamicCall("InsertFile(QString&)", Questioner); // A .docx file that i created. It's written "Question:" inside of it.
                              selection->dynamicCall("InsertFile(QString&)", Question[i]); // Array of .docx files that i created. Works fine.
                          }
                              selection->dynamicCall("InsertFile(QString&)", Footer); 
                              document->dynamicCall("SaveAs(const QString&)", Name);
                              document->dynamicCall("Close()");
                              axObject.dynamicCall("Quit()");
                          

                          And it came out like this
                          Header
                          Questions:
                          { insert docx to this area }
                          Questions:
                          { insert docx to this area }
                          ...
                          Questions:
                          { insert docx to this area }
                          Footer
                          .
                          Is there a way to just insert "Question" + " " char(i+48) + ": ": to the start of every questions that i make ? Additionally, as you can see my output, after every "Question:" it just goes down one line ( i do understand that it's because i insert the file, but i dont know how to fix it ) and i dont want that.

                          1 Reply Last reply
                          0
                          • D Dante Pham

                            @Andy314 I am using ActiveX but i have no idea what functions are used to do what you describe. Would you mind giving me some examples ? How do I copy and paste ? How to hide the window ?

                            Andy314A Offline
                            Andy314A Offline
                            Andy314
                            wrote on last edited by
                            #33

                            Hello @Dante-Pham,
                            I have no Qt-Code for Word, in the moment - this was for an other projekt in CPP-Builder for what I had a WrapperClass, but for Access and Excel I made my own tools.

                            Obj ist the "Excel.Application" QAxObject*
                            void setVisible(bool on=true)
                            {
                            Obj->setProperty("Visible", on);
                            }
                            I think for the Wordapp it is the same.
                            You can generate html-Documentation for a QAxObject
                            QString s = Obj->generateDocumentation();
                            Here you get a least the function and property names, and an idea what the do. How the really work is detective searching work in the internet. The most matches you get if you look for VBA-Code (Visual-Basic for Application).

                            1 Reply Last reply
                            1
                            • A Offline
                              A Offline
                              Asperamanca
                              wrote on last edited by
                              #34

                              For anyone revisiting this topic, I believe I have found a good alternative approach:
                              https://forum.qt.io/topic/85893/generating-docx-communicating-with-word-add-in/10

                              1 Reply Last reply
                              2
                              • A Offline
                                A Offline
                                Awais Hafeez
                                wrote on last edited by
                                #35

                                @Dante-Pham,

                                I would like to inform you that with 'Aspose.Words for C++' API, you can easily join, merge or append multiple Word files (DOCX, DOC, RTF, ODT etc) into one (see Joining and Appending Documents). I am copying here a simplified code snippet so that you can have a quick look:

                                // load source & destination files
                                System::SharedPtr dstDoc = System::MakeObject(dir + u"destination.docx");
                                System::SharedPtr srcDoc = System::MakeObject(dir + u"source.docx");
                                    
                                // set the appended file to start on a new page. (optional)
                                srcDoc->get_FirstSection()->get_PageSetup()->set_SectionStart(Aspose::Words::SectionStart::NewPage);
                                // append the source file using the original styles found in the source file.
                                dstDoc->AppendDocument(srcDoc, Aspose::Words::ImportFormatMode::KeepSourceFormatting);
                                // save result
                                dstDoc->Save(dir + u"output.docx);
                                

                                So, 'Aspose.Words for C++' API makes this process very simple as well as configurable, providing the means to control how the files are joined. Moreover, 'Aspose.Words for C++' does not depend on or utilize Microsoft Word; so, it is not required to install MS Office. You can even integrate this native C++ API within your Qt application.

                                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