How to replace content in template docx document and Open XML SDK 2.0 (Aug 09)?

Matthew Sharpe picture Matthew Sharpe · Sep 1, 2009 · Viewed 38.4k times · Source

I have a "template" docx document which contains the desired layout, and wish to insert content using C#, but I cannot find a way to uniquely address specific sections of the document, such as paragraphs or tables. What is the best way to uniquely identify elements in the document? Thanks, Matt Sharpe.

Answer

Ahmad Mageed picture Ahmad Mageed · Sep 3, 2009

How is your template built? Does it use an underlying XML Schema loaded as part of the *.docx? Or are you using content controls off of the Developer ribbon, in which case each control is uniquely identified by a given tag name? Both of these approaches would make identifying certain sections of your document easier as you could control where tables or paragraphs would be.

Also, you may want to consider using the Open XML SDK 2.0 (uses .NET 3.5). It includes a handy Document Reflector tool that allows you to open up and inspect any Open XML document and shows how to generate the code for any element you click on.

Apart from that, to learn more about content controls you can check these posts: