none
How to update a content control? RRS feed

  • Question

  • I am new to OpenXML SDK. Please could someone advise me how to update the content controls in a word document programmatically using C#.

    I had a look at some samples and they all seem to add a CustomXML and then bind to the content controls.

    Is there anyway I can update the content controls without using a CustomXML? I have data in a separrate xml.  I want to loop through the xml below and locate the name of the content control in word and update its value.

    <ContentControl Name="1XXX" Value ="YYYY">

    <ContentControl Name="2XXX" Value ="YYYY">

    My content controls are NOT place in table.

    Thanks

    SS


    Shiva Sadayan

    Friday, August 31, 2012 2:22 PM

Answers

  • Shiva,

    Just like Tom said. But the harder part here is to maintain the integrity of a document. There are different situations of handling (the content of) content controls. Sometimes a CC contains a paragraph, sometimes a paragraph contains a CC. On other occasion a table cell contains a CC and wise versa. You can't always just replace the CC content and it is quite hard to create a robust document generation solution and you can quite easily end up with a corrupt document. You will have to find all these different combinations/cases. You will also have to handle new line and tab characters in the text you are inserting. Take a look at this product which solves exactly your problem.

    -- Jure

    Monday, September 3, 2012 10:31 AM

All replies

  • Hi Shiva,

    Thanks for posting in the MSDN Forum.

    I suppose you have named every controls in your document. In my scenario, I would recommend you enumerate the SdtProperties via the name and find SdtBlock/SdtRun node of this specific name. Then you can update date content controls value under this path SdtBlock/SdtRun -> SdtContentBlock-> Paragraph (this is a option, perhaps there will not have this node) -> Run -> Text.

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 3, 2012 5:43 AM
    Moderator
  • Hi Tom,

    Please could you point me to some code samples.

    Thanks


    Shiva Sadayan

    Monday, September 3, 2012 7:57 AM
  • Shiva,

    Just like Tom said. But the harder part here is to maintain the integrity of a document. There are different situations of handling (the content of) content controls. Sometimes a CC contains a paragraph, sometimes a paragraph contains a CC. On other occasion a table cell contains a CC and wise versa. You can't always just replace the CC content and it is quite hard to create a robust document generation solution and you can quite easily end up with a corrupt document. You will have to find all these different combinations/cases. You will also have to handle new line and tab characters in the text you are inserting. Take a look at this product which solves exactly your problem.

    -- Jure

    Monday, September 3, 2012 10:31 AM
  • Hi Shiva,

    I will mark Jurij's reply. If you feel it can't help you please feel free to unmark it.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 10, 2012 7:10 AM
    Moderator