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. Finding and replacing in MS Word via QAxObject
Forum Updated to NodeBB v4.3 + New Features

Finding and replacing in MS Word via QAxObject

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 631 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.
  • O Offline
    O Offline
    Oda412
    wrote on last edited by
    #1

    Good day! I try to replace t1 to t2 in document test1.docx and save the result in document test2.docx.
    But there is an error: "QAxBase::dynamicCallHelper: Selection: No such property in [???????? Microsoft Word 97?2003]..."
    Could you, please, help me to solve this problem?
    Best regards, Olga

    #include "widget.h"
    #include <QAxObject>
    #include <QAxWidget>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QAxWidget word("Word.Application");
        word.setProperty("Visible", true);
        QAxObject * documents = word.querySubObject("Documents");
        QAxObject * document=documents->querySubObject("Open(QString)","C:/Users/Olga/Desktop/test.docx");
    
        QAxObject * sel = document->querySubObject("Selection");
        QAxObject * find = sel->querySubObject("Find");
    
        QString sOld = "t1";   // change from this
        QString sNew = "t2";   // to this one at the time
    
        find->dynamicCall("Execute(QString)",sOld);
        sel->dynamicCall("TypeText(QString)",sNew);
    
        document->dynamicCall("SaveAs (const QString&)", QString("C:/Users/Olga/Desktop/test2.docx"));
        document->dynamicCall("Close (boolean)", false);
        word.dynamicCall("Quit (void)");
         return 0;
    }
    
    1 Reply Last reply
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by hskoglund
      #2

      Hi, the find and replace command in MS Word is a bit convoluted, try something like:

      auto find = document->querySubObject("Content")->querySubObject("Find");
      
      // some params for the find/replace call
      bool bMatchCase      = false;
      bool bMatchWholeWord = false;
      bool bMatchWildCards = false;
      bool bReplaceAll     = true;
      
      QString sOld = "t1";   // change from this
      QString sNew = "t2";   // to this one at the time
      
      QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1");
      
      find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
      
      O 2 Replies Last reply
      4
      • hskoglundH hskoglund

        Hi, the find and replace command in MS Word is a bit convoluted, try something like:

        auto find = document->querySubObject("Content")->querySubObject("Find");
        
        // some params for the find/replace call
        bool bMatchCase      = false;
        bool bMatchWholeWord = false;
        bool bMatchWildCards = false;
        bool bReplaceAll     = true;
        
        QString sOld = "t1";   // change from this
        QString sNew = "t2";   // to this one at the time
        
        QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1");
        
        find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
        
        O Offline
        O Offline
        Oda412
        wrote on last edited by
        #3

        @hskoglund , thank you a lot!
        As you can see - I've taken your code from one of your previous posts and helplessly tried to do something :)
        Now, with your help it works!

        Wish you all the best
        Olga

        P.S.: Soon I'll post new couple of questions - but in another topics..

        1 Reply Last reply
        0
        • hskoglundH hskoglund

          Hi, the find and replace command in MS Word is a bit convoluted, try something like:

          auto find = document->querySubObject("Content")->querySubObject("Find");
          
          // some params for the find/replace call
          bool bMatchCase      = false;
          bool bMatchWholeWord = false;
          bool bMatchWildCards = false;
          bool bReplaceAll     = true;
          
          QString sOld = "t1";   // change from this
          QString sNew = "t2";   // to this one at the time
          
          QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1");
          
          find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)",vl);
          
          O Offline
          O Offline
          Oda412
          wrote on last edited by
          #4

          Good day!
          Do you know, how to do the same thing via OpenOffice or LibreOffice?
          Thank you in advance!
          Olga

          JonBJ 1 Reply Last reply
          0
          • O Oda412

            Good day!
            Do you know, how to do the same thing via OpenOffice or LibreOffice?
            Thank you in advance!
            Olga

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Oda412
            You can't! Well at least if you are asking about this thread, since it's ActiveX to communicate with MS Word.

            You would have to find an API for LibreOffice. I am not an expert, maybe start out from https://api.libreoffice.org/examples/examples.html ?

            1 Reply Last reply
            2

            • Login

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