views:

135

answers:

1

I'm trying to use a docx read in via the OpenXML SDK as template in a .Net web app for form letters (essentially). I have a docx with the formatting I want, marked up with XML Schema tags defined from a xsd (whatever they're called, the help is exceedingly useless on this).

The only thing similar I found on Google was what seemed to be replacing the CustomXmlBlock elements with text elements with the desired data. This seems... messy and fragile.

I also have the added complication that some of the data is hierarchical, with elements for sections, and then items in that section. This would seem to completely break the above method.

So, can someone point me in the right direction for doing this, or suggest a better way to handle templating form letters?

+5  A: 

There are a number of choices:

  1. What you're doing, XSD Schema mapping. This was introduced in Word 2003. It was really only truly active in that version of Word as when Word 2007 came out, the preferred new method of forms became #2.
  2. Content Controls. More on this below.
  3. Tried and true are Bookmarks. If you're interested, there is an article here.

For Content Controls 1, the concept is simple: wrap your content controls in the hierarchy needed. That is to say, for example, have a Date Picker Content Control and a Rich Text Content Control and then group them. Then inside the Rich Text Content Control, place a Combo Box Content Control. Eric White has written a great starter article on this at retrieving content from inside of Content Controls at Processing all Content Parts in an Open XML WordprocessingML Document. This article is a much more comprehensive introduction to Content Controls in it's links Using Open XML WordprocessingML Documents as Data Sources.

More links on Content Controls:

  1. Brian Jones' site has a lot of great articles. I believe these were his first: The Easy Way to Assemble Multiple Word Documents and Create a rich Word document based on your own custom XML (without the need for XSLT).
  2. The Word Content Control Toolkit. Probably the best tool around for working with Content Controls.
  3. Visual How To Content. Many articles here deal with Content Controls.

1 To work with Content Controls manually in the Word client, enable the "Developer" Ribbon from Options and then insert Content Controls and set their properties from the left-hand section of the Controls group in the Developer Ribbon.

Otaku
Wow, that's excellent, I wasn't even aware Content Controls existed. I'm going to accept this and award the bounty, though I have one question, well more like a confirmation. It appears that there's no in-built support for any sort of repeating structure (tables, lists, etc) with Content Controls. I assume I put in a Rich Text Content Control into the doc as a placeholder, then generate the table or list using the OpenXML sdk and set it as the content of the content control?
Matt S
@Matt S: I've put in a few extra links that should also help you get started. There are a number of ways to do repeaters with Content Controls - one is what you mentioned. The other way is to use Building Blocks. Another way is to kind of do the opposite of what you mentioned - put a table with just a header row and then create rows populated with CCs in the cells. Do take a look at the Word Content Control Kit as well - that will save your life in working with CCs until you become much more familiar.
Otaku