Important: Please read the Qt Code of Conduct -

Replacing Field values in Word Document QAxObject

  • Dear all,

    I'm really new to QT and I was tasked to update some field values in word document programmatically, currently I can replace text from word document fine, but when that field value is inside an object (table or anything) it's not working, my code is:

            QString     outFile("D:\\#test files\\output.docx");
            QString     inFile1("D:\\#test files\\input.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");
            auto find = selection->querySubObject("Find");
            QString sOld = "${name}";
            QString sNew = "Ibrahim";
            bool bMatchCase = false;
            bool bMatchWholeWord = false;
            bool bMatchWildCards = false;
            bool bReplaceAll = true;
            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);
            document->dynamicCall("SaveAs(const QString&)", outFile);

    If you can help it would be awesome :)

    Best regards,

  • @ivasija-evonem
    You may not a useful answer here, as I don't think this has anything to do with Qt/QAxObject. You need to either read up in what you need to do in Word to achieve this, or try your code from a Word VBA macro/recording from Word, to determine what really needs to be done to find the items correctly.

Log in to reply