Traditionally, developers have used VBA to create Office based applications. When .NET came along with all its goodies, they felt the need for creating document-centric solutions with .NET. To address this need, Microsoft released Visual Studio Tools for Microsoft Office (VSTO), an add-on to Visual Studio .NET 2003 to create managed applications with Word 2003 or Excel 2003.
Visual Studio Tools for Office 2003 (VSTO) is a great tool to build Office solutions in .NET. Unfortunately, Office 2000 users cannot use this tool. This article is an attempt to fill that void. It shows you how to build Office 2000 solutions using Visual Studio .NET 2003. I have tried to keep the code as close as possible to VSTO 2003 so that you can easily migrate to Office 2003.
I will walk you through the process of creating a simple Word application using Visual Basic .NET. We are going to build an application consisting of a Windows Form which accepts user data and this data displayed on the Word document.
Fire up Visual Studio .NET, use the File | New | Project menu item, and select Class Library project template as shown below:
Use Project| Add References menu. Chose the COM tab and add a reference to Microsoft Word 9.0 object library.
Public Class OfficeCodeBehind
Friend WithEvents ThisDocument As Word.Document
Friend WithEvents ThisApplication As Word.Application
#Region "Initialization code"
Public Sub New()
Public Sub Startup(ByVal application As Object, ByVal document As Object)
ThisApplication = CType(application, Word.Application)
ThisDocument = CType(document, Word.Document)
Public Sub Shutdown()
ThisApplication = Nothing
ThisDocument = Nothing
Private Sub ThisApplication_DocumentOpen(ByVal Doc As Word.Document)
MsgBox("Application event Document open")
Private Sub ThisDocument_New() Handles ThisDocument.New
MsgBox("inside Document new managed code")
Create a class called
OfficeCodeBehind. Create variable declarations for Application (
ThisApplication) and main document (
Create initialization code which consists of routines
Startup routine is called to initialize
ThisDocument variables when the user opens a Word Document, and
ShutDown routine is called when user closes the Word document to perform cleanup. In addition, create event handlers for basic events and a routine which writes string to Word.
Create a Windows Form which calls
WriteStringToWordDocument routine of
OfficeCodeBehind class to write text to Word.
Fire up Word. Use the Tools | Macro menu item and select Visual Basic Editor. Add a reference to WordTest1.tlb file by selecting Tools|References and clicking on the Browse button as shown below:
Dim app As New WordTest1.OfficeCodeBehind
Private Sub Document_Close()
Private Sub Document_Open()
app.Startup Me.Application, Me
Add the above code in Visual Basic editor in
The above code is the glue which connects the Word Document to the .NET class library we created. When the Word document is opened, it fires the
Document_Open event which in turn calls the
StartUp method of
OfficeCodeBehind. It passes to
Startup a reference to the Application (
Word.Application) and a reference to the Document (
Startup method copies this information into local variables and hooks up event handling.
The Shutdown method is called when the document closes to perform any cleanup.
Creating Office Projects using Word and Excel Project Templates
Note: Please read the instructions in ProjectTemplateSetup.rtf to setup Project Templates before reading any further.
Fire up Visual Studio .NET. Use the File | New | Project menu item, and select Word Document project template as shown below:
Private Sub ThisDocument_Open() Handles ThisDocument.Open
MsgBox("Document Open from Managed code")
Add the above code in the Code Editor. Build the project. Follow the same steps outlined for the Class Library project above to hook up the Word Document to the Word Project we created in .NET.
- Added Word and Excel Project Templates on Nov 20 2004.
- Created a Setup package to install Project Templates on Dec 1 2004.
I highly recommend the articles below for Office development using .NET:
Future enhancements and feedback
I am planning to do the following enhancements when time permits:
- Create a Word Add-in (which mimics VSTO 2003 loader OTKLOADR.DLL) to automatically load the DLL which is set in Word document's Custom properties.
- Create C# version of the project.
Feel free to email me your suggestions and comments. I would like to make improvements based on your feedback and your high ratings :).