0

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);
    document->dynamicCall("Close()");
    axObject.dynamicCall("Quit()");

If you can help it would be awesome :)

virusivv
  • 337
  • 2
  • 5
  • 17
  • I would suggest dropping the word "fields" from your title because what you are replacing is specific text, not a Word field. You will get more pertinent attention that way. If you can change the nature of the target files, you would do well replacing your targets with real Word DocVariable or DocProperty fields. Then use your code to change the variable or properties and update the related fields in the documents. Some document properties (under the Quick Parts > Document Properties menu) are mapped to XML data points and do not require updating of fields if those are used. – Charles Kenyon Nov 17 '20 at 14:00
  • actually we are creating fields: Insert -> Quick Parts -> Field, then selecting the type of the field, maybe this is wrong but I don't know how to make other templates cause the word document must contain some placeholder where I can programmatically add some values – virusivv Nov 17 '20 at 14:26
  • The placeholder can be (1) a DocVariable field or (2) a DocProperty field. You can change the variables or properties using code and then update the field. You can also use one of the *mapped* DocProperty Content Controls in which case there is no need to update a field if the property is changed. It is automatic. More on these in my related page: http://addbalance.com/word/MappedControls.htm – Charles Kenyon Nov 17 '20 at 16:08
  • hey dude thanks, can you please write this as a solution so I can mark it as answer :D – virusivv Nov 19 '20 at 09:23

1 Answers1

1

If you can change the nature of the target files, you would do well replacing your targets with real Word DocVariable or DocProperty fields. Then use your code to change the variable or properties and update the related fields in the documents. Some document properties (under the Quick Parts > Document Properties menu) are mapped to XML data points and do not require updating of fields if those are used.

The placeholder can be (1) a DocVariable field or (2) a DocProperty field. You can change the variables or properties using code and then update the field.

You can also use one of the built-in mapped Document Property Content Controls in which case there is no need to update a field if the property is changed. It is automatic. More on these in my related page: Repeating Data Mapped Document Property Content Controls or Other Mapped Content Controls.

Here are links to two Word MVP pages on accessing Document Properties using vba.

Charles Kenyon
  • 869
  • 1
  • 8
  • 19