Easily utilize Microsoft Word functionality in your .NET application

While the Microsoft Office suite still relies on VBA and COM, it may be utilized in .NET code via .NET and COM interoperability. This allows you to create more powerful applications by integrating the many functions available in the Word products into your application.

Developers often think they need they need to reinvent the wheel, when all they really need to do for certain tasks is make use of existing applications. A good example is utilizing one or more applications in the Microsoft Office suite of products. For instance, you may want to utilize an Excel spreadsheet to create a chart or an expense report, or create a Word document that contains user-entered data. In this article, I'll examine integrating Word in a .NET application.

The programming model

It is strange that Microsoft pushes .NET as the ultimate solution, yet it is not utilized within the Microsoft Office programming model; Office still uses the older VBA (Visual Basic for Applications) model. A critical aspect of VBA is that it is based on COM (Component Object Model), so .NET and Microsoft Office cannot natively communicate with each other. However, a .NET feature called COM interop provides callable wrappers to allow .NET and COM to interoperate.

A runtime callable wrapper allows a COM component to be used by .NET. If you are using the Visual Studio .NET IDE, follow these steps:

  1. Select Add Reference from the Project menu.
  2. Select the COM tab of the Add Reference window and double-click the appropriate type library file listed.
  3. Select OK to end and add the reference.

At this point, Visual Studio .NET converts the objects and members in the COM type library file into equivalent .NET assemblies. Once the .NET assemblies are generated, you can easily instantiate classes and call members as if the COM objects and members were native .NET classes and members. This process may also be reversed so you can utilize a .NET assembly in a COM-based application, but that is beyond the scope of this article.

The process if further illustrated with an example. Let's create a Word document from a simple .NET Windows Form.

Using Microsoft Word

First, I create a new project within Visual Studio .NET and add a reference to the Microsoft Word type library. I am using Microsoft Word 2003, so the type library is Microsoft Word 11.0 Object Library. Once the reference is added, I can use the Word objects in the code. The VB.NET sample in Listing A opens an existing Word document from the local drive when the user clicks a button on the Windows Form.

Here are a few notes about the code:

  • The System.Runtime.InteropServices namespace is imported to work with COM and .NET interoperability. It contains the COMException class.
  • The .NET wrapper for the Word objects is contained in the Microsoft.Office.Interop.Word namespace (the reference previously added to the project).
  • The Application class within the Word namespace is used to access the Word application.
  • The Document class within the Word namespace allows you to work with Word documents.
  • The Open method contained within the Documents property of the application allows an existing document to be loaded. It contains a Close method as well.
  • The Activate method of the Document class opens the document within a new instance of Word.
  • The code accessing the Word document is contained within a Try/Catch block. It catches COM errors via the COMException class.
  • The MessageBox.Show method replaces the Office VBA MsgBox function.

Listing B contains the equivalent C# code. The major difference in the C# code from the VB.NET version is the call to Documents.Open requires all parameters are passed to it, and these parameters must be references to objects. For this reason, objects are created and assigned values. Also, only references (ref) are passed to the method call. In addition, many of the parameters are null so the Type.Missing value is used in their place. In addition, the actual Word application must be activated via its own Activate method, and it must be displayed by setting its Visible parameter to true. The last caveat is that C# recognizes the backslash (\) as an escape sequence, so double backslahes (\\) must be utilize to include a single one in a string.

Populating Word documents via .NET

You can open an existing Word document, or you may want to create a new Word document and populate it with data from an application. You can create a new Word document via the Add method of the Documents collection. The following VB.NET snippet uses the objects created in the previous example:


In addition, the Word Object Model includes numerous methods and properties for working with text within a Word document. The VB.NET example in Listing C creates a new document and inserts text in the beginning of the document when a button is selected (only the code for the button is shown).

A few notes about this sample:

  • The Word.Range object allows you to work with a group or range of text within a Word document. The place within a document is specified by starting and ending values. In this example, the beginning of the document (start at zero) is used. An end value indicates the size of the range—zero indicates the beginning as well.
  • The Text property of the Word.Range object allows the text to be set or reset.
  • The SaveAs method allows you to save a document using the specified value.

Listing D contains the C# equivalent. Once again, C# requires all parameters be specified so the Type.Missing value is utilized. You can browse the Word Object Model to learn more about the methods and properties available.

Another development tool

Microsoft Office is the most popular office suite in the world. You can easily integrate the power of the suite into your .NET application via .NET and COM interoperability. It opens a world of possibilities for adding powerful functionality to an application.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox