Select Page

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:

  1. Must expose the same functionality that MS Word provides in performing document creation, management, and mail merging functions.
  2. Must not rely on Microsoft Word.
  3. Must not employ Automation (explained below), because I wanted to avoid having to learn the MS Word object model.
  4. Must provide a simple interface to use so that I could easily incorporate the component into my application.
  5. 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 https://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…

Create and manage MS Word Documents with Jisys WordReports

Photo credit to MakeUseOf

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.

  1. A registered copy of MS Word is required on the system where my web application will run.
  2. 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:

// Create instance of the WordReport Wordwriter component

WordWriter doc = new WordWriter();  
doc.WriteLine(“Hello World”);                       // Hello World
doc.Save(“MyDocument.doc”);                    
// Save the document

Three lines of code are all it took to duplicate the same functionality we attempted with Word Automation. Now, that’s what I call efficiency!

In the introduction section of this article, I mentioned my effort in developing a web application that would use mail merging functionality. The code listed below is an excerpt of how I incorporated WordReports into a control (.ascx) file to create mailing labels. I simply ‘cut-n-pasted’ the code directly from samples provided by Jisys and made the appropriate modifications. Everything worked perfectly the first time I compiled the program. As a note, I am using Microsoft’s IBuySpy portal solution as the framework for my application.

  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");
  }

Summary

If you have any need to generate Word documents in your .NET application, you can’t go wrong with WordReports. The product documentation includes detailed coding examples and tutorials that will have you up and running with this component in no time. To Jisys, great job and I look forward to more innovative products from your company.

About the author

For nearly 19 years, Joél Contreras has been employed in the Information Technology area of a major retirement and financial firm. During this tenure, he has played significant roles in possibly every area of the company’s technological organization including Database Management System, Network Infrastructure, Project Management, Disaster Recovery, and Systems Programming. Although he has been in management for over fourteen years, he has never lost the passion for “playing” with new technology and thus, it is no wonder he has embraced the .NET framework and C#. Joél has a BA in Computer Information Systems and a Master of Business Administration. He can be reached at jc@loanhost4life.com

Share This