Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Fill a MS Word object with multiple data values



  • Hello,

    I am working on filling a MS Word document with dynamic data from my program. I did use fields, which are working with a blank MS Word template and I can write multiple values but now that I am trying to fill a document that is designed template it is not working. I tried by using Document Property and it is working with only one value. Do you have any suggestion on how can I fill the document with multiple Document Property values, or any other suggestion on how to fill the MS Word object with values?

    The code that I used for Document Property is:

        QAxObject* auth = document->querySubObject("BuiltInDocumentProperties(\"Subject\")");
        auth->dynamicCall("Value", "hello world");


  • @alfe
    I don't understand what you mean by/are trying to do with "fill the document with multiple Document Property values".

    You show an example which works, to set the Subject doc prop. What are trying to do, which does not work, against what specific doc prop with what multiple values?



  • I do have for example :
    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);
    selection->dynamicCall("TypeText(const QString&)", "");
    QAxObject* auth = document->querySubObject("BuiltInDocumentProperties("Subject")");
    auth->dynamicCall("Value", value1);
    auth->dynamicCall("Value", value2);
    auth->dynamicCall("Value", value3);

    All these data need to be written dynamically on Word file. I did attach an image with the way it looks. In this case value1 and value2 are inserted as Document Property/Subject from Quick Parts and when I am trying to write on them by passing values from my program, they are getting the same value. Do you have any suggestion on what can I use in order to write on these three fields, the specific value which is meant to be shown.

    Capture.JPG



  • @alfe said in Fill a MS Word object with multiple data values:

    they are getting the same value.

    QAxObject* auth = document->querySubObject("BuiltInDocumentProperties("Subject")");
    auth->dynamicCall("Value", value1);
    auth->dynamicCall("Value", value2);
    auth->dynamicCall("Value", value3);
    

    Because you are setting the same doc prop, Subject, 3 times. It will, of course, end up with the last one, value3.

    I really don't understand what you are trying to do/expecting. Doc prop Subject must be string, so what are you expecting to have "multiple values"?

    Find whatever it is you say have "multiple values" in the Word/VBA documentation.

    Or, and I think I've said this before to people. Sometimes when i wanted to figure how to VBA something in Word which I wanted to do but didn't know how. I switch on "Record macro" in Word. Did my stuff; end recording. Then looked at the VBA it generated.



  • @JonB

    @JonB said in Fill a MS Word object with multiple data values:

    Because you are setting the same doc prop, Subject, 3 times. It will, of course, end up with the last one, value3.

    This is where I am stuck. I am not having an idea how to pass unique values for each variable to the document even though I am trying for days. I took the "Subject" as an example. What I do really need is to know how to write those values (value1, value2 and value3) in my word template. Do you have any suggestions if you think that what I am doing is not the right way?

    Thank you very much for your suggestions and time. And I do hope that this time I did manage to make it more clear what I am asking for.



  • @alfe
    OK, I think I now understand what you want to do.

    There are no "multiple data values".

    You are using BuiltInDocumentProperties("Subject"). Word documents have a number of predefined, "bultin" properties. Subject is one of them, there are others. But you cannot your own to the "builtin" ones.

    What you want is Word's Custom properties. You can add as many of these as you like to a document. they have your own name, and a (string) value. I forget exactly how, but you can also "insert" them into a document's text if you want. There's a menu item for this, it comes out as something like \{MyCustomPropName}. Word replaces these with their value if you put them in your document.

    You want to do something like have your own named Value1, Value2 etc. Then from your code you will do similar to what you have done but something like:

    document->querySubObject("CustomDocumentProperties(\"Value1\")");
    // or
    document->querySubObject("CustomDocumentProperties()");
    auth->dynamicCall("Value1", value1);
    auth->dynamicCall("Value2", value2);
    

    (It won't be exactly as I have written. No point copying that, you need to look up the correct syntax/methods.)

    As I said before, search Google to find out, or record a macro inside Word when you set them interactively to see what it generates. You access a dialog where you can create/set your own Custom Properties from within Word via something on the document/file as a whole. You should go find that so you understand what we mean.

    That should give you enough. Assuming of course I'm right that this is what you're really trying to do :)



  • Thank you for your suggestions, they were helpful. I just wanted to update you and in case someone else needs the information that it worked by using bookmarks as solution!


Log in to reply