Browse DevX
Sign up for e-mail newsletters from DevX


Activate Your Word Documents with VSTO : Page 2

Word processors were originally designed to be the staging area for printed documents, but years ago people began to read and distribute documents online. So why, then, do we still have flat documents burdened by the legacy of the printer? Find out how VSTO can change all of that.





Building the Active Document
Within Visual Studio.NET you can create a new Word Document containing C# code behind using the File —> New Project dialog, selecting 'Office' as the application type and selecting 'Word Document' as the template as in Figure 2.

Figure 2. Creating a New Word Document Project: Select Office as the application type and Word Document as the template.
Author's Note: Make sure that you have no Office documents open when you do create the new project, because if you do, you'll get an alert asking you to activate scripting—and if you have any open Office documents at that time activation will not succeed.

The newly created project contains your document (for example WordDocument3.doc) and an associated code-behind file called ThisDocument.cs. You can effectively treat the document as a Windows Forms application at this point. Your event handlers and other code will go into ThisDocument.cs. Interestingly, you can also treat Visual Studio.NET as a word processor, because this project type incorporates the Word toolbar at the top of the document; in other words, you can type and lay out the document's text as if you were using Word directly. Figure 3 shows the document within the Visual Studio editor.

The chart in this document was done in the traditional way; I imported the data into Excel and created a chart, then cut and pasted the image of the chart into the document. You could, as a follow-up exercise, see if you can create a dynamic chart in Word!

With the document open in the editor, you can do more than just type and format text—you can also place controls on it as if it were a Windows form. Go ahead and drag-and-drop a MonthCalendar control and a DataGridView control onto the document. Make the layout similar to Figure 4.

With the controls in place, the problem now becomes a simple case of writing code to handle the users' interactions with these controls.

Figure 3. Word Processing Inside Visual Studio: Microsoft Word project types let you type and edit text into the document in Visual Studio just as if you were using Word directly.
Before writing the code, don't forget to add a Web reference to the Web service you created earlier. In this example, the reference (and thus the proxy class that represents it) is called localhost.

When the user selects a day from the calendar, you'll need to supply a DateChanged event handler for the CalendarMonthView control that calls the Web service and gets the detailed sales list for the selected day. Here's the code:

private void monthCalendar1_DateChanged( object sender, DateRangeEventArgs e) { string strDate = monthCalendar1. SelectionStart.ToShortDateString(); localhost.Service mySvc = new localhost.Service(); String st = mySvc.GetData(strDate); DataSet ds = ConvertXMLToDataSet(st); DataTable dt = ds.Tables[0]; dataGridView1.DataSource = dt; dataGridView1.Refresh(); }

The DateChanged event handler pulls the current date out of the calendar using the SelectionStart property, and calls the GetData Web service method, passing the date as a parameter. The service returns a string containing serialized XML data, which the handler converts into a DataSet using the ConvertXMLToDataSet helper function that you'll see in a moment. Next it retrieves the first DataTable from the DataSet and binds it to the DataGridView. All this is extremely simple and straightforward.

Figure 4. Adding Controls to the Document: The document acts as if it were a Windows Form, letting you can drag and drop controls onto the design surface in the editor.
Here's the ConvertXMLToDataSet helper function that deserializes the XML string back into a DataSet:

public DataSet ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); // Load the XmlTextReader from the stream reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS; } catch (Exception ex) { string strTest = ex.Message; return null; } finally { if (reader != null) reader.Close(); } }

The result is that when you launch the document, you can view the sales information as a summary by reading the graph, and by selecting dates on the calendar, you can drill down to specific days to see the sales information for that day. The beauty of this application is that it surfaces information that was previously unavailable from within an old-style, paper-oriented "passive" document.

The ubiquity of Office makes it an enormously powerful platform on which you can deliver new applications that add value to your business information and business logic. In this article you saw how to use Microsoft Word to provide active, rich documents to your clients, shortening their workflow and giving them new ways to interact with your data. There isn't anything drastically new about this—it's just a new way at looking at how people work. As the majority of us use Office on a daily basis there is a vast realm of possibilities for developers to innovate and make use of the platform in new and interesting ways. This article detailed one simple, yet powerful, use case. If you want to give your company, or yourself, an edge over the competition, the opportunity is there—go out, have fun and start innovating with Office's rich, powerful application platform!
