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#
For More information
http://openxmldeveloper.org/
http://www.codeproject.com/KB/cs/GenerateWord2007.aspx
http://en.wikipedia.org/wiki/Office_Open_XML
http://msdn.microsoft.com/en-us/magazine/cc163526.aspx#S1
We will replace the place holders with values from the Orders table of the Northwind database.
Step 2 – Create an OrderInformation class
This 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);
// Open the new Package
Package pkg = Package.Open(filename, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be read
Uri uri = new Uri("/word/document.xml", UriKind.Relative);
PackagePart part = pkg.GetPart(uri);
XmlDocument xmlMainXMLDoc = new XmlDocument();
xmlMainXMLDoc.Load(part.GetStream(FileMode.Open,FileAccess.Read));
xmlMainXMLDoc.InnerXml = ReplacePlaceHoldersInTemplate(orderInfo, xmlMainXMLDoc.InnerXml);
// Open the stream to write document
StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write));
//doc.Save(partWrt);
xmlMainXMLDoc.Save(partWrt);
partWrt.Flush();
partWrt.Close();
pkg.Close();
}
private string ReplacePlaceHoldersInTemplate(OrderInformation orderInfo, string templateBody)
{
//#ADDRESS#
templateBody = templateBody.Replace("#ADDRESS#", orderInfo.FirstName);
//#CITY#
templateBody = templateBody.Replace("#CITY#", orderInfo.City);
…
}
Step 4 – Implement a web or WinForms application utilizing the above classes
Here is a sample button_Click handler:
protected void btnFrom_Click(object sender, EventArgs e)
{
string orderID = “1234”;// lstOrders.SelectedValue;
//generate documet
OrderInformation orderInfo;
GenerateDocument doc = new GenerateDocument();
orderInfo = doc.GetOrderInformation(orderID);
doc.GenerateWord(orderInfo);
}