views:

1298

answers:

5

Hi,

I have a need to populate a Word 2007 document from code, including repeating table sections - currently I use an XML transform on the document.xml portion of the docx, but this is extremely time consuming to setup (each time you edit the template document, you have to recreate the transform.xsl file, which can take up to a day to do for complex documents).

Is there any better way, preferably one that doesn't require you to run Word 2007 during the process?

Regards Richard

+2  A: 

I tried myself to write some code for that purpose, but gave up. Now I use a 3rd party product: Aspose Words and am quite happy with that component.

It doesn't need Microsoft Word on the machine.

"Aspose.Words enables .NET and Java applications to read, modify and write Word® documents without utilizing Microsoft Word®."

"Aspose.Words supports a wide array of features including document creation, content and formatting manipulation, powerful mail merge abilities, comprehensive support of DOC, OOXML, RTF, WordprocessingML, HTML, OpenDocument and PDF formats. Aspose.Words is truly the most affordable, fastest and feature rich Word component on the market."

DISCLAIMER: I am not affiliated with that company.

splattne
Yikes, that price is pretty steep!
Mark Kadlec
+1  A: 

Since a DOCX file is simply a ZIP file containing a folder structure with images and XML files, you should be able to manipulate those XML files using our favorite XML manipulation API. The specification of the format is known as WordprocessingML, part of the Office Open XML standard.

I thought I'd mention it in case the 3rd party tool suggested by splattne is not an option.

Philipp Schmid
+1  A: 

Use invoke docx lib. it supports table data (http://invoke.co.nz/products/help/docx_tables.aspx). More info at http://invoke.co.nz/products/docx.aspx

A: 

Have you considered using VB? You could create a separate assembly to populate your document.

I know you are looking for a C# solution, but the XML literal support is one area where XML literal support could help you populate the document. Create a document in Word to server as a template, unzip the docx, paste the relevant XML section you want to change into you VB code, and add code to fill in the parts you wish to change. It's difficult to say from your description if this would meet your requirements but I would suggest looking into it.

Peter Tate
+1  A: 

Have you considered using the Open XML SDK from Microsoft? The only dependency is on .NET 3.5.

Documentation: http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx

Download: http://www.microsoft.com/downloads/details.aspx?familyid=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

Goyuix