Replacing Content Controls in OpenXML

Freelance Jobs

I need something as a placeholder. I at first looked to Content Control as a solution but I'm having some problems with it.

I then looked into adding CustomXML to the .docx but turned away from that because of the i4i lawsuit.

Then I decided I would just plain change the text of the Content Control through OpenXML SDK 2.0 but even if it's so marked the Content Control doesn't go away. I guess that it doesn't know that the text changed unless it happens inside word.

I could perhaps just remove the CC and place text instead but I'm afraid of problems with format and styles it could bring and also it would kind of defy the purpose of the Content Control.

Then I started wondering if I could define my own placeholders that Word could recognize. Through Building blocks perhaps. It doesn't have to do anything except be easy to find using OpenXML and somehow taggable so I know what to replace it with. I'm not really sure what can be done with Building Blocks but I'm hoping it's do-able.

Not sure what solution would be best for me but what I need is:

a)Something that's easy to place in the template, perhaps predefined Content Control placeholders that you can place where you wan't and style as you like.

b)When the data has been added it removes all placeholders, it won't be modified again. It keeps the style/format defined in the placeholder.

TO RECAP, I need answer to either

How can I edit Content Controls in OpenXML SDK so they will be removed after text is added.


Can I define my own custom OpenXML tag for a Word Document that I could then replace?

Ingó ValsH
2010-08-10 11:12:41 Scores:4

3 answers

Answer 1

Perhaps this can help you,

private void DeleteSdtBlockAndKeepContent(MainDocumentPart mainDocumentPart, string sdtBlockTag)
        List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
        SdtBlock sdtA = null;

        foreach (SdtBlock sdt in sdtList)
            if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == sdtBlockTag)
                sdtA = sdt;

        OpenXmlElement sdtc = sdtA.GetFirstChild<SdtContentBlock>();
        OpenXmlElement parent = sdtA.Parent;

        OpenXmlElementList elements = sdtc.ChildElements;

        var mySdtc = new SdtContentBlock(sdtc.OuterXml);

        foreach (OpenXmlElement elem in elements)

            string text = parent.FirstChild.InnerText;

Bilel Boughanmi H
2010-08-19 08:39:56
Yes thank you, already did something similiar and I placed tables inside of blocks for iterated text and other inside sdtRun and replaced them based on Data XML wich I had a path to set in the contents tag.
Ingó Vals H
2010-08-19 09:38:12 Scores:0
Answer 2

Take a look at using a Field. The mail merge fields are designed for exactly this purpose.

David Thielen H
2011-06-10 01:41:11
I will check that out. While I did get it to work with stdRun and stdBlock and such it doesn't always appear as such when the Doc is created.
Ingó Vals H
2011-06-10 09:39:21 Scores:0
We do this in our reporting system (Windward) and it works fine. Check the validity of the docx file you are creating closely.
David Thielen H
2011-07-17 01:59:38 Scores:0
Answer 3

I don't understand from your question if you are only interested in a solution that automatically removes the ContentControl/SDT when you modify it using the OpenXML SDK, or whether you want it to disappear after the content is modifed programmatically or by a user.

If the former, I think you'll have to remove it yourself as Bilel suggested. If the latter, you should look at this property: ContentControl.Temporary ("the ContentControl is automatically deleted when the user types in the control, or when the text in the control is changed programmatically. When the ContentControl is automatically deleted from the document, the text in the control remains in the document.")

DGGenuine H
2011-12-30 00:44:40
Share |
View original post at

Related topics

Replacing Word's Open File Dialog in a COM Add-in

I'm writing a Word COM Add-in that replaces the Open & Save dialogs with my own. For the save dialog, I'm handling the documentBeforeSave event from the application events. This works fine. For the open dialog, there is no such event, so I'm currently handling the onClick of the Open... ...

Binding Data to Word 2007 Content Controls Using Visual Studio Tools for the Office System (3.0)

I found this article - Binding Data to Word 2007 Content Controls Using Visual Studio Tools for the Office System (3.0) - and thought that that's exactly what I'm trying to do. I want to programatically build a product brochure using Content Controls and Open XML. The article ...

OpenXML: Allow editing of Content Controls in locked Word document

I want to create a Word document that works as a template, where all the document is locked from editing except the Content Controls (<sdt/> elements) in the document that the user can edit. What I've seen is that if I lock the document edition (right now I'm using the _markAsFinal ...

Target header and footer Content Controls in Word 2007

I'm trying to edit a .docx header through Word Automation. If the Content Controls are placed in the body of the text there is no problem, but if the content controls are in the header or footer I'm not able to target them. Is there any way of targetting Content ...

OpenXML: Creating reusable Content Controls with binding

I need to attach specific XML to a word document so the elements will show up in the right places. This I can do with OpenXML SDK. The thing is I want too be able to define the Content Controls already bounded in some sort of template so other people can ...

OpenXML insert text into content control Word 2007

I am learning OpenXML. I have been looking for hours trying to find how to do a simple task: insert text into a content control in c#. I have a template document with two controls "Name" and "Age." I can find them well enough, but I just cannot add ...

How to get the list of all content controls in the document?

I am using interop and I want to get the list of all content controls contained in word document (in the body, shapes, header, footer..). Is this the correct and the best way to do this : public static List<ContentControl> GetAllContentControls(Document wordDocument) { if (null == wordDocument) throw ...

Replacing Text with a picture in Word with C#

I'm trying to replace my Text with a picture that I created (QRCode.png) first method() { //creates QRCode Bitmap b = qCodeEncoder.Encode(encodable); //saves it b.Save("QrCode.png", System.Drawing.Imaging.ImageFormat.Png); FindAndReplace(WordApp, "<QRCode>", b); } Second method is a normal replacer for text that ...

Replacing content in Word 2010 Textboxes using OpenXML

Using the Open XML SDK I've been successful in programatically finding bookmarks or text strings in a word document and inserting new content. I'm using OpenXmlPowerTools.SearchAndReplacer to do the text search and replace and this post's answer for the bookmarks Replace bookmark text in Word file using Open ...

Word Content Controls -> What do the arrows at the side of a content control mean?

I am creating a word document with plain text content controls. I have added a few of these and got code working that substitutes value from an XML file into the content controls. However now when i add a content control to the template I can see whats like a ...