Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Office 2000 Solutions using .NET

0.00/5 (No votes)
1 Dec 2004 1  
This article shows you how to create Office 2000 solutions using Visual Studio .NET 2003.

Introduction

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.

Getting Started

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.

Imports Word
    
Public Class OfficeCodeBehind
    Friend WithEvents ThisDocument As Word.Document
    Friend WithEvents ThisApplication As Word.Application

#Region "Initialization code"
    ' Default constructor.

    Public Sub New()
    End Sub
    Public Sub Startup(ByVal application As Object, ByVal document As Object)
        ThisApplication = CType(application, Word.Application)
        ThisDocument = CType(document, Word.Document)
    End Sub
    
    Public Sub Shutdown()
        ThisApplication = Nothing
        ThisDocument = Nothing
    End Sub
    
    Private Sub ThisApplication_DocumentOpen(ByVal Doc As Word.Document) 
            Handles ThisApplication.DocumentOpen
        MsgBox("Application event Document open")
    End Sub

    Private Sub ThisDocument_New() Handles ThisDocument.New
        MsgBox("inside Document new managed code")
    End Sub
#End Region

End Class

Create a class called OfficeCodeBehind. Create variable declarations for Application (ThisApplication) and main document (ThisDocument) objects.

Create initialization code which consists of routines Startup and Shutdown. The Startup routine is called to initialize ThisApplication and 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()
  app.ShutDown
End Sub

Private Sub Document_Open()
  app.Startup Me.Application, Me
End Sub

Add the above code in Visual Basic editor in ThisDocument.

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 (Word.Document). The 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:

'Called when the document is opened

Private Sub ThisDocument_Open() Handles ThisDocument.Open
    MsgBox("Document Open from Managed code")
End Sub

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.

History

  • Added Word and Excel Project Templates on Nov 20 2004.
  • Created a Setup package to install Project Templates on Dec 1 2004.

References

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 :).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here