Modifying Microsoft Office Word Content Control OpenXml

In my case I had a Microsoft Office Word document which contains content place controls which needs to be filled by specific data after specific business action.

The idea is to select all the descendants from type “SdtElement” which has a tag name specified and then compare it with the provided tag name.

Next, remove all the text from the content control and add our new value.

Sample code:

private void ModifyDocumentPlaceHolder(Document document, string tagName, string value)
            // Find all placeholders with Tag name
            var placeHolders = document.MainDocumentPart.RootElement.
                Where(P => P.Elements<SdtProperties>().FirstOrDefault().
                    Elements<Tag>().FirstOrDefault() != null);

            // Select the required element by tag name
            var requiredPlaceHolder = placeHolders.FirstOrDefault(P =>
                Elements<Tag>().FirstOrDefault().Val == tagName);

            // Remove all text from the place holder
                delegate(Text textObj)
                    textObj.Text =

            // Add the required text
            requiredPlaceHolder.Descendants<Text>().FirstOrDefault().Text = value;


I hope that helped


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s