views:

755

answers:

3

I want to set up a template document along the lines of this:

================
Doc content

----------------
Merge Field1
Merge Field2
Merge Field3
----------------

More doc content
================

I then want to be able to open the document, load a DataTable from the database in C# and merge the fields in the template section in the middle. The DataTable will have multiple records so it needs to merge and output multiple templates to create one big document. I can't find any examples of doing this.

A: 

I've used this library to generate MS Word 2007 documents. Hope it'll help you. P.S. Previously this library was completely free but now they've added a commercial version too. But free version contains all necessary features for you.

zihotki
+2  A: 

If you are looking for information about programmatically replacing the Mail Merge fields using C# code then this article might help.

+1  A: 

I had a job doing this stuff a while back. We were using Word Automation but it's painful (because Word will do crazy things like pop up a modal dialog which will break your code).

We moved to using the Aspose library. I found it fairly reasonable and quite fully featured in this area (there is good support for merge fields). It is, however, commercial.

(I have no ties to the company - I've just used their software)

Edit: If you only need Word 2007 support, don't bother with these guys; there are plenty of free libraries. Support for older versions is harder to find though

Alun Harford