oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Programming Word from .NET

by Jesse Liberty

A .NET medical office program would have many components and would be quite complex. One small part of such a program might solve a problem that many doctors have: when the patient is given a worrisome diagnosis it is hard to process all the other information the doctor is providing (prognosis, treatment, medications, etc.). It would be great if the program could generate a letter summarizing what was said, and mail it to the patient that day, for review once the initial shock wears off.

One way to accomplish this is to couple a .NET program with the power of Microsoft Word, delegating to Word that which it does best (formatting, printing, document management). You could, of course, use a very sophisticated mail-merge from within Word itself, but remember we want to store the patient information in a database, and we want to integrate this one module into a larger .NET program. Thus, we will use each tool for its own strengths:

  • Word: Creating rich text documents
  • SQL Server: Storing and retrieving data
  • .NET: Creating forms, interacting with the user, and tying the various pieces together

Invoking Word from .NET--Getting Started

There are a few steps you must take to begin working with Word (or any office application) from within Visual Studio, and there is (of course) more than one way to do it. We'll take one of the simpler approaches. Begin by navigating to the MSDN site to download the Office XP Primary Interop Assemblies. Follow the directions to install the assemblies.

The key to working with Word is to add a reference in your project, from the COM tab for the Word Object Library, as shown in Figure 1.

Add a reference to word
Figure 1. Adding a reference to Word

You can now access, create, and manipulate Word documents using the Microsoft.Office.Interop.Word .Application object as well as the Microsoft.Office.Interop.Word .Document doc as will be described in context below.

An Application in Three-Part Harmony

The three parts of this demonstration application (kept intentionally and strictly simple to focus on the key aspects) will be:

  • A Word document. This is a letter to the patient that we will modify and customize for each individual visit.
  • A set of tables in SQL Server. You may download the script files to create the database from my website.
  • A .NET 2.0 Windows application

The Database

Let's begin by examining the subset of simplified database tables we'll need, as diagrammed in Figure 2.

db diagram
Figure 2. Database diagram

The central table is Patients, which we can assume will contain extensive demographic information about each patient with links to the patients' office visits, illnesses, and medications. To facilitate this, we add three more tables: MedicationNames, Diagnoses, and Visits. Visits is in a one-to-many relation with Patients (each patient can have many visits, but each visit has only one patient). The other two are in many-to-many relationships (e.g., one medication can be prescribed for many patients, and each patient can have many prescriptions) and so are mediated by intermediate tables, PatientToMedication and PatientToDiagnoses, respectively.

Programming ASP.NET

Related Reading

Programming ASP.NET
Building Web Applications and Services with ASP.NET 2.0
By Jesse Liberty, Dan Hurwitz

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow