Iterate through all Contentcontrols in a word document

How to iterate through all the content controls in a word document and how to assign dynamic values to the content controls based on the content control title?

Hi Minu,

Try this:

var document = DocumentModel.Load("input.docx");

foreach (IContentControl contentControl in document.GetChildElements(true,
    ElementType.InlineContentControl,
    ElementType.BlockContentControl))
{
    ContentControlProperties properties = contentControl.Properties;
    string title = properties.Title;
    string value = "TODO";

    CharacterFormat format = properties.CharacterFormat ?? new CharacterFormat();
    contentControl.Content.LoadText(value, format);
}

document.Save("output.docx");

Regards,
Mario

Thanks Mario. But this is not fetching the Repeating Section Content Control and content controls created inside a repeating section content control from the document . Is there a way to include this type of content control too?

Unfortunately no.
I’m afraid that repeating content controls are currently not supported.

Okay Mario. In that case do you have any other way to add dynamic sections under a particular main section in the word document like below?

1. Main Section
1.1 Sub Section1
1.2 Sub Section2

Hi,

Please check the GemBox.Document’s mail merge feature, more specifically the merge range option.

By defining a merge range you can specify an area that will end up being duplicated for each record in some data source of yours.
The linked example shows how you can use it to create a dynamic number of rows in the table, but you can use it for creating any arbitrary content that you want.

I hope this helps.

Regards,
Mario