Introduction
For several months, I searched for a .NET component that would allow me to easily create and manage Word documents as well as incorporate mail merging functionalities into my web application. In my mind, the component had to meet these minimal requirements:
- Must expose the same functionality that MS Word provides in performing document creation, management, and mail merging functions.
- Must not rely on Microsoft Word.
- Must not employ Automation (explained below), because I wanted to avoid having to learn the MS Word object model.
- Must provide a simple interface to use so that I could easily incorporate the component into my application.
- Must allow royalty free distribution of the assembled component. The fee I charge for my web application is based on an Application Service Provider model, thus, I did not want to add any additional costs to the base price.
Naively enough (in retrospect), I did not think that these requirements were so far fetched! After all, I was certain that someone else must have had this set of requirements at one point or another. My first stop was the www.asp.net. I did not find a component that even came close to meeting my requirements. I then turned to the rest of the web and searched in the major search engines for what I thought was a product that I would definitely find. To my surprise, my efforts in finding a solution to my problem were futile.
Through a colleague I heard about a product, under development at the time, which could potentially meet my requirements. I immediately looked up the company on the web and to my disappointment found the site ‘under development.’ Well, Like Pavlov’s dog, I eagerly anticipated getting my hands on this product for almost two months. Like clock work I visited the site at least once a week looking for the announcement of this .NET component. Finally, in July of this year, the site became available and I was able to download and test this component.
This article presents a brief product review of the WordReports .NET component by Jisys. In my humblest of opinions, this company has successfully identified and filled a market gap in the generation and manipulation of Word documents without relying on MS Word and Automation. Because the folks at Jisys have done an outstanding job in documenting the features and “how to’s” of this product, I will not be redundant and present all of the code samples I tested. Instead, I will point you to their web site at http://www.jisys.com. There you will find well documented examples and tutorials that will guide you in the installation and use of this product. It took me less than 10 minutes to incorporate the WordReports component into my application to instantly generate mailing labels (excerpt code listed below).
What is Automation?
Before we delve into WordReports, let’s discuss what Automation is and how to incorporate it into a .NET application. Prior to the introduction of WordReports, creating mail-merged documents using Microsoft .NET managed code meant (1) using MS Word, and (2) relying on Automation as a “bridge” into the classes and methods made available by the MS Word object model. But what is Automation? Well, simply stated, Automation is a process that allows a program written in a language, such as C# or VB.NET, to control other Microsoft applications (such as Word, Excel, or PowerPoint) programmatically. Thus, tasks that are normally performed via the user interface can be executed by the controlling program. Candidate tasks for Automation can include creating new MS Word documents, opening an existing document, adding text to documents, performing MS Word mail-merge functions, etc…
How does Automation work?
Automation is accomplished by using a collection of classes and methods exposed by the program that you would like to assume control of. In MS Word, the COM object model is the bridge to using the functionalities typically available via the Word user interface. Access to this object model begins with establishing a reference to the type library. I will demonstrate how this is done in Visual C# .NET. Bare in mind that this can be performed from any of the .NET managed languages. Anyway, here are the steps I followed to add the Word COM object into my project:
1. Go to the solutions explorer in Visual Studio .NET.
2. Right click on the references system folder.
3. Select “Add Reference” from the pop-up options.
4. In the Add Reference dialog, click on the COM tab.
5. Search for the Microsoft Word component and click on the Select button.

Now, with the Word COM assembly in the application, the different types of functions available to the MS Word via the user interface can be performed. Here’s an example of how to open a new MS Word document, append some text to it, save it, and quit MS Word.
The first thing we need to do is create an instance of MS Word in our application with a declaration statement like this:
Word.ApplicationClass myWordApp = new Word.ApplicationClass();
With the instance in place, we can call the methods and properties provided by MS Word in creating, managing, and manipulating Word documents.
Word.Document myWordApp =
myWordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
myWordApp.Activate(); // start Word
myWordApp.Selection.TypeText(“Hello World”); // Enter the text
myWordApp.Section.TypeParagraph();
myWordApp.SaveAs(“c:\\HelloWorld.Doc); // Save the file
myWordApp.Application.Quit(ref missing, ref missing, ref missing); // Quit Word
That did not seem too difficult. However, there are two issues that forced me to move away from Automation.
- A registered copy of MS Word is required on the system where my web application will run.
- I have to learn the Word object model in order to perform the most rudimentary functions.
In my case, these two issues were in contrast to my original requirements as stated in the introduction section. First of all, I do not want to have to worry about MS Word licensing fees and requirements for every web solution I distribute. Secondly, learning the Word object model was not an appetizing thought to me. I needed a simpler solution and found it in WordReports.
WordReports
With WordReports, I was able to easily create Word documents and reports without having to rely on Microsoft Word or Automation. WordReports supports all MS Word page layouts and exposes a simple object model which gave me complete control of Word document generation. Via the object model I was able to programmatically manage page layouts, font sizes, styles, colors, document headers and footers. In addition, I tested several table templates with minimal effort and incorporated images, such as company logo’s, into the reports. I can’t say enough about how pleased I was when testing this product.
Anyway, all of WordReports functions are exposed via an extremely simple interface. In fact, here’s the same example previously discussed in the Automation section:
WordWriter doc = new WordWriter();
doc.WriteLine(“Hello World”);
doc.Save(“MyDocument.doc”);
public void Make_Labels(object sender, System.EventArgs e)
{
// Obtain contact information from the Investor table
InvestorsDB Investors = new InvestorsDB ();
SqlDataReader reader = Investors.GetInvestors(ModuleId);
// Create instance of the WordReports Word writer.
WordWriter writer = new WordWriter();
// Spacing.
writer.Paragraph();
// Initiate.
writer.Setfont(fontFace.Arial, 9);
// Cell count.
int pos = 0;
// Decrease default margins by 50%
writer.MarginTop /= 2;
writer.MarginBottom /= 2;
writer.MarginLeft /= 2;
writer.MarginRight /= 2;
// Compute cell width.
int width = (writer.PaperWidth -
(writer.MarginLeft + writer.MarginRight)) / 3;
// Row count.
int rows = 0;
// Read the results.
while(reader.Read())
{
// New row?
if(pos == 0)
{
// Start the table row.
writer.Row();
}
// Get the fields.
string companyName =
reader["Investor_Company_Name"].ToString();
string address =
reader["Investor_Company_Address"].ToString();
string city =
reader["Investor_Company_City"].ToString();
string state =
reader["Investor_Company_State"].ToString();
string postalCode =
reader["Investor_Company_Zip_Code"].ToString();
// Write this address.
writer.Cell(width);
writer.WriteLine(companyName);
writer.WriteLine(address);
writer.WriteLine(city + ” ” + state + ” ” + postalCode);
writer.WriteLine(“”);
writer.EndCell();
// Increment.
pos++;
// New row?
if(pos == 3)
{
// End row.
writer.EndRow();
// Increment row.
rows++;
// End of page?
if(rows > 9)
{
// New page.
writer.PageBreak();
writer.Paragraph();
rows = 0;
}
// Reset.
pos = 0;
}
}
// Pending row?
if(pos > 0)
{
// End the row.
writer.EndRow();
}
// Close the reader.
reader.Close();
// Spacing.
writer.Paragraph(2);
string LabelFolder = Server.MapPath(“MailingLabels”);
string filedate = DateTime.Now.ToLongDateString();
writer.Save(
LabelFolder + “/” + “CustomerLabels” + filedate + “.doc”);
}
The answer is going to depend slightly upon if the application is running on a server or if it is running on the client machine. If you are running on a server then you are going to want to use one of the XML based office generation formats as there are know issues when using Office Automation on a server [1].
However, if you are working on the client machine then you have a choice of either using Office Automation [2] or using the Office Open XML format [3, 4, 5], which is supported by Microsoft Office 2000 and up either natively or through service packs. One draw back to this though is that you might not be able to embed some kinds of graphs or images that you wish to show.
The best way to go about things will all depend sightly upon how much time you have to invest in development. If you go the route of Office Automation there are quite a few good tutorials out there that can be found via Google and is fairly simple to learn. However, the Open Office XML format is fairly new so you might find the learning curve to be a bit higher.
[1] Considerations for server-side Automation of Office – http://support.microsoft.com/kb/257757
[2] Office Development Center – http://msdn.microsoft.com/en-us/office/default.aspx
[3] Office Open XML – http://en.wikipedia.org/wiki/Office_Open_XML
[4] OpenXML Developer – http://openxmldeveloper.org/default.aspx
[5] Introducing the Office (2007) Open XML File Formats – http://msdn.microsoft.com/en-us/library/aa338205.aspx