This is a step by step instruction on how to dynamically generate MS Word 2007 documents.

Background on Word 2007 Document Internals
Word 2007 document is based on the Office Open XML file formats. As you will see, the Office Open XML file formats are based on standard ZIP file technology. Each top-level file is saved as a ZIP archive, which means you will be able to open a Word document just as you would any other ZIP file and look inside.
Note that the 2007 Microsoft Office system applications such as Word and Excel have introduced file extensions for documents that use the new format. For example, the .docx extension is used for Word documents stored in the Office Open XML file formats while the more familiar .doc extension continues to be used for Word docs stored in the binary format.

Let’s create a new document in Word 2007 and add "Hello Word" as the text. Save the document using the default format to a new file named Hello.docx and close Word. Next, locate Hello.docx using Windows Explorer and rename it Hello.zip. Open Hello.zip and see the structure of folders and files that Word has created inside (see Figure).

Figure 1 DOCX File is a ZIP Archive

The top-level file (Hello.docx) is known as a package. Since a package is implemented as a standard ZIP archive, it automatically provides compression and it makes its contents instantly accessible to many existing utilities and APIs on both Windows platforms and other platforms alike.

Inside a package there are two kinds of internal components: parts and items. In general, parts contain content and items contain metadata describing the parts.

A basic package contains an XML file called [Content_Types].xml at the root, along with three directories: _rels, docProps, and a directory specific for the document type (for example, in a .docx word processing package, there would be a word directory). The word directory contains the document.xml file which is the core content of the document.

A part is named using a Uniform Resource Identifier (URI) that contains its relative path within the package file combined with the part file name. For example, the main part within the package for a Word document is /word/document.xml. Here are a few more typical part names you will find inside the package for a simple Word document:

/[Content_Types].xml
This file describes the contents of the package. It also contains a mapping for file extensions and overrides for specific URIs.

_rels
This directory contains relationships for the files within the package. To find the relationships for a specific file, look for the _rels directory that is a sibling of the file, and then for a file that has the original file name with a .rels appended to it. For example, if the content types file had any relationships, there would be a file called [Content_Types].xml.rels inside the _rels directory.

/_rels/.rels
This file is where the package relationships are located. Applications look here first. Viewing in a text editor, one will see it outlines each relationship for that section. In a minimal document containing only the basic document.xml file, the relationships detailed are metadata and document.xml.

/docProps/core.xml
This file contains the core properties for any Office Open XML document.

/word/document.xml
This file is the main part for any Word document – which we will be manipulating in our code.

The Project

We will be using Visual Studio 2008, .NET Framework 3.5, the NorthWind database and a simple Word template in our application.Step 1 – Create a Word 2007 template.
Create a simple Word 2007 file with placeholders enclosed between two #’s (like #ORDERID#) and save it as ‘MyTemplate.docx’
Below is the content I used:

From: My Company
#ADDRESS#
#CITY#, #REGION#, #POSTCODE#
#COUNTRY#
Dear #CUSTOMERID#
We're pleased to inform you that your order number #ORDERID# is going to be shipped on #SHIPPEDDATE#.
If you have any questions or comments, please feel free to give us a call at #HOMEPHONE#
Regards,
#FIRSRNAME# #LASTNAME#
#TITLE#

We will replace the place holders with values from the Orders table of the Northwind database.Step 2 – Create an OrderInformation classThis class simply represents an Order object with properties for OrderID, CustomerID,….Step 3 – Create a GenerateDocument class
A main method, public void GenerateWord( OrderInformation orderInfo), is implemented to generate the new document named after the value of OrderID of the current Order.
In this class, System.IO.Package is used to replicate the template, replacing the place holder variables in the /word/document.xml file with the actual values from the current Order.
Below is the code:
public void GenerateWord( OrderInformation orderInfo)
{
string templateDoc = string.Format("{0}{1}", ConfigurationManager.AppSettings["TemplateFolder"], ConfigurationManager.AppSettings["TemplateFile"]);
string filename = string.Format("{0}{1}.docx", ConfigurationManager.AppSettings["OutputFolder"], orderInfo.OrderID);

// Copy a new file name from template file
File.Copy(templateDoc, filename, true);

Dan,Nice intro, what I need to know now is what happens when you get a field that isn't populated, ie empty. How do you get to remove the tag and also the line so there are no messy spaces in the resulting document. I have tried replacing with empty string, but that still leaves the space.

Gemys62, It doesn't matter where you where you read the values from (database, XML file, flat file, ...) just construct the OrderInformation object and pass it to the GenerateWord( OrderInformation orderInfo) method.

Take a look at Docentric Toolkit (http://www.docentric.com/), a commercial tool for mail merge, document generation and MS Word reporting in .NET based on the similar concepts which offers:- Superior mail-merge features such as: data bound images, charts, (arbitrary level of) nested tables/bulleted list, subdocuments, horizontal tables, headers & footers etc. - Very original approach for designing templates called "visual mail-merge": with the help of special MS Word Add-In and point-and-click technique you can bind template data tags against any property path in a .NET object graph which is data source of the template. This way designing of templates becomes very efficient since you always have a transparent list of broken binding paths. In short - it provides ultimately the best user experience for template design on the market today! - Document Generation is OpenXML based, stable and fast and can be implemented in only a few lines of code. - Can be used as alternative to reporting tools where it is desired to enable end-users to design/maintain templates. - Can be used in cloud based solutions.

This is a .NET Library for Word that i think can solve almost every problem that has been mentioned above. It uses C#/.NET/VB.NET to create, read, modify and even convert word files to many other file formats. I hope it will solve your problem.