none
How to recalculate fields after Range.InsertXML() RRS feed

  • Question

  • Hi there,
    I hope this is the correct forum to post this question.

    What I'm trying to do:
    I'm trying to load the XML-Representation(WordML) from an OLE-Object, modify it and then load it back into the OLE-Object and finally display it to the User. And I want to do this in C++

    What I already managed to do:
    I was able to load the XML via ActiveDocument->Range()->GetXML(false);
    I parsed the structure and modified it. (Replaced docvariables with their newly calculated values)
    I loaded it back into the OLE-Object via ActiveDocument->Range()->InsertXML(xml);
    And I displayed it to the user.

    Where the problem is?
    The Document, I'm parsing, not only contains docvariables, but  also conditional form elements(IF).
    The precalculated results are also part of the XML (They follow the <w:fldChar w:fldCharType="seperate"> -Tag).
    If I don't replace them, then there will be still displayed the old value.
    If I simply omit this block, nothing is displayed.
    The "w:dirty" attribute, which tells Word to  recalculate this fields result, is only working when reading the XML from a file, but when using Range()->InsertXML() it's completely ignored.
    If I call ActiveDocument->GetFields()->Update(); after inserting the XML, it seems that the XML hasn't been parsed yet and all docvariables are replaced by their old values.

    So is there some way to make Word parse this new XML? Or do I really have to calculate the IF's myself? 

    I'll be really happy if someone knows a way to do, what I'm trying to do :)

    Thanks a lot. 

    Monday, January 16, 2012 7:14 PM

Answers

  • <<
    If you're already automating the application through the UI, why not use that to replace the doc variable information and update the fields?
    Cindy Meister, VSTO/Word MVP

    I'm not displaying the document to the user through a GUI. I let Word display it to the user.>>

    I didn't say a "GUI" I said the "UI", meaning the Word user interface. Since you're automating the Word user interface, why don't you simply change the value of the document variable using the method you're using to get and insert the XML? I can't "do" C++, but I'd think it would be something like

    ActiveDocument->Variables()->getItem("docVar")->setValue("xyz");

    followed by

    ActiveDocument->GetFields()->Update();


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by lSoleyl Tuesday, January 17, 2012 5:06 PM
    Tuesday, January 17, 2012 4:11 PM
    Moderator

All replies

  • openxmldeveloper.org might be the better place to ask this as that's where you'll find the specialists.

    But if you could at least give us an example of the field code combination involved...?

    If you're already automating the application through the UI, why not use that to replace the doc variable information and update the fields?


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 17, 2012 8:08 AM
    Moderator

  • Thanks for the answer,

    Here is the an example field code:

    {IF "{ DOCVARIABLE docVar}" <> "-1" "{ DOCVARIABLE docVar}" ""}

    And here is, how the corresponding XML looks like, after my program set the values of the DOCVARIABLES.

    <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="begin"/></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText> IF "</w:instrText></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="begin"/></w:r>
        <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText> DOCVARIABLE docVar </w:instrText></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="separate"/></w:r>
        <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText>parsed_docVar</w:instrText></w:r> 
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="end"/>
      </w:r><w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText>" &lt;&gt; "-1" "</w:instrText></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="begin"/></w:r>
        <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText> DOCVARIABLE docVar </w:instrText></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="separate"/></w:r>
        <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText>parsed_docVar</w:instrText></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="end"/></w:r>
      <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:instrText>" ""</w:instrText></w:r>
    <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="separate"/></w:r>
      <w:r><w:rPr><w:noProof/><w:sz w:val="24"/></w:rPr><w:t>&lt;&lt; docVar &gt;&gt;</w:t></w:r>
    <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="end"/></w:r>
    
    Where
    <w:r><w:rPr><w:sz w:val="24"/></w:rPr><w:fldChar w:fldCharType="separate"/></w:r>
    <w:r><w:rPr><w:noProof/><w:sz w:val="24"/></w:rPr><w:t>&lt;&lt; docVar &gt;&gt;</w:t></w:r>
    

    is the precalculated value of this field, that Word should replace by the new Value parsed_docVar.

    If you're already automating the application through the UI, why not use that to replace the doc variable information and update the fields?
    Cindy Meister, VSTO/Word MVP

    I'm not displaying the document to the user through a GUI. I let Word display it to the user.



    • Edited by lSoleyl Tuesday, January 17, 2012 12:28 PM
    Tuesday, January 17, 2012 11:48 AM
  • <<
    If you're already automating the application through the UI, why not use that to replace the doc variable information and update the fields?
    Cindy Meister, VSTO/Word MVP

    I'm not displaying the document to the user through a GUI. I let Word display it to the user.>>

    I didn't say a "GUI" I said the "UI", meaning the Word user interface. Since you're automating the Word user interface, why don't you simply change the value of the document variable using the method you're using to get and insert the XML? I can't "do" C++, but I'd think it would be something like

    ActiveDocument->Variables()->getItem("docVar")->setValue("xyz");

    followed by

    ActiveDocument->GetFields()->Update();


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by lSoleyl Tuesday, January 17, 2012 5:06 PM
    Tuesday, January 17, 2012 4:11 PM
    Moderator
  • Yeah,
    it seems I will have to do it like you said.

    I wanted to avoid setting the docvariables through the OLE interface, because I hoped I could get a better performance if I would only work on the XML string. But it seems that doing it through OLE is the only way to change the fields correctly.

    Too bad :(


    • Edited by lSoleyl Tuesday, January 17, 2012 5:11 PM
    Tuesday, January 17, 2012 5:06 PM
  • Well, as I said, you could check with the specialists at openxmldeveloper.org. This forum is for the Word API, not the Open XML file format, so if there's a "trick" you're missing with that (it doesn't appear to be the case, however) then openxmldeveloper.org would be the place to ask.

    However, if you're processing only a single doc variable then I don't think there will be a signicant difference in processing time as you're still reading/writing the XML int the Word UI. So you have at least three calls to the Word UI, plus the time Word requires to re-process the layout on-screen when you InsertXML. If you have to write only one DocVariable, then update the fields, that's only two calls.


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 17, 2012 5:19 PM
    Moderator
  • I've posted this problem on openxmldeveloper.org right after you gave me the advice to do so, but until now I've got no reply.

    Unfortunately the documents this program works with can have about one hundred docvariables. For now I'll simply use your solution. 
    Maybe someone finds someday a way to solve this problem :)

    Thank you for the help

    Tuesday, January 17, 2012 7:16 PM
  • In which forum section did you ask? I don't see your question in either the Word or the Open XML section...

    There's also an Open XML SDK forum here on MSDN, but generally, the people who answer at OpenXMLDeveloper.org have more practical experience.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 18, 2012 2:07 PM
    Moderator
  • The Question is in the C/C++ Forum

    But now I reposted it in the WordML section.

    • Edited by lSoleyl Thursday, January 19, 2012 10:09 PM
    Thursday, January 19, 2012 10:04 PM