Click here to Skip to main content
Click here to Skip to main content

Creating Office 2000 Solutions using .NET

, 1 Dec 2004
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) .

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

Share

About the Author

srirambalaji

United States United States
Sriram works as Senior Technical Specialist at Infosys's Microsoft Technology Center.

He has been working on ASP.NET,VB.NET,C#,ASP and SQL Server for past 7 years. He loves to churn out innovative ideas.
 
During his freetime,you may find him hiking, humming a song in his karaoke or listening to inspiring music. You can reach him at srirambalaji_work@yahoo.com.

Comments and Discussions

 
QuestionUse VS 2005, file assembly not found Pinmemberdewacp1-Dec-07 23:01 
QuestionHow To Use It In VS2005? Pinmemberari.sty28-May-07 3:58 
AnswerRe: How To Use It In VS2005? Pinmembersrirambalaji13-Nov-07 4:49 
Generalusing : .net win32/api hooking and this concept PinmembertheGhost_k814-Jun-06 1:30 
Generalusing -> .net win32/api hooking and this concept PinmembertheGhost_k814-Jun-06 1:30 
Questionerror Pinmemberdave1823-May-06 7:22 
Generaloffice 2000 project deployement Pinmemberfatih isikhan2-Apr-06 21:46 
Questionbring the program and the template to a different computer PinmemberMapsoft131-Jan-06 5:16 
AnswerRe: bring the program and the template to a different computer Pinmembersrirambalaji31-Jan-06 7:23 
QuestionRe: bring the program and the template to a different computer PinmemberMapsoft131-Jan-06 10:41 
AnswerRe: bring the program and the template to a different computer Pinmembersrirambalaji1-Feb-06 21:01 
GeneralRe: bring the program and the template to a different computer PinmemberMapsoft12-Feb-06 0:43 
GeneralAccess to .Net from word 2000 via a WLL PinsussBill Brotherton15-Aug-05 20:25 
Questiontlb file? PinmemberJeff T.5-Jan-05 6:08 
AnswerRe: tlb file? Pinmembersrirambalaji7-Jan-05 11:18 
GeneralProject setup Pinmembern4nilesh22-Nov-04 7:11 
GeneralRe: Project setup Pinmembersrirambalaji23-Nov-04 9:42 
GeneralRe: Project setup Pinmembern4nilesh24-Nov-04 3:34 
GeneralRe: Project setup Pinmembersrirambalaji24-Nov-04 4:08 
GeneralRe: Project setup Pinmembern4nilesh24-Nov-04 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 1 Dec 2004
Article Copyright 2004 by srirambalaji
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid