Click here to Skip to main content
15,867,870 members
Articles / Programming Languages / Visual Basic
Article

Creating Office 2000 Solutions using .NET

Rate me:
Please Sign up or sign in to vote.
3.88/5 (22 votes)
1 Dec 20043 min read 147.6K   1.4K   45   20
This article shows you how to create Office 2000 solutions using Visual Studio .NET 2003.

Image 1

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:

Image 2

Use Project| Add References menu. Chose the COM tab and add a reference to Microsoft Word 9.0 object library.

VB
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:

Image 3

VB
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:

Image 4

VB
'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


Written By
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 Pin
dewacp1-Dec-07 22:01
dewacp1-Dec-07 22:01 
QuestionHow To Use It In VS2005? Pin
ari.sty28-May-07 2:58
ari.sty28-May-07 2:58 
AnswerRe: How To Use It In VS2005? Pin
srirambalaji13-Nov-07 3:49
srirambalaji13-Nov-07 3:49 
Generalusing : .net win32/api hooking and this concept Pin
K edar V14-Jun-06 0:30
K edar V14-Jun-06 0:30 
Generalusing -> .net win32/api hooking and this concept Pin
K edar V14-Jun-06 0:30
K edar V14-Jun-06 0:30 
Questionerror Pin
dave1823-May-06 6:22
dave1823-May-06 6:22 
Generaloffice 2000 project deployement Pin
fatih isikhan2-Apr-06 20:46
fatih isikhan2-Apr-06 20:46 
Questionbring the program and the template to a different computer Pin
Mapsoft131-Jan-06 4:16
Mapsoft131-Jan-06 4:16 
Hi,

I wrote a program that works fine on my computer.
Now I want to bring the program and the template to a different computer.
What do I have to do? What files needed to be copied where? Is there anything else to install?

Thanx
AnswerRe: bring the program and the template to a different computer Pin
srirambalaji31-Jan-06 6:23
srirambalaji31-Jan-06 6:23 
QuestionRe: bring the program and the template to a different computer Pin
Mapsoft131-Jan-06 9:41
Mapsoft131-Jan-06 9:41 
AnswerRe: bring the program and the template to a different computer Pin
srirambalaji1-Feb-06 20:01
srirambalaji1-Feb-06 20:01 
GeneralRe: bring the program and the template to a different computer Pin
Mapsoft11-Feb-06 23:43
Mapsoft11-Feb-06 23:43 
GeneralAccess to .Net from word 2000 via a WLL Pin
Bill Brotherton15-Aug-05 19:25
sussBill Brotherton15-Aug-05 19:25 
Questiontlb file? Pin
Jeff T.5-Jan-05 5:08
Jeff T.5-Jan-05 5:08 
AnswerRe: tlb file? Pin
srirambalaji7-Jan-05 10:18
srirambalaji7-Jan-05 10:18 
GeneralProject setup Pin
n4nilesh22-Nov-04 6:11
n4nilesh22-Nov-04 6:11 
GeneralRe: Project setup Pin
srirambalaji23-Nov-04 8:42
srirambalaji23-Nov-04 8:42 
GeneralRe: Project setup Pin
n4nilesh24-Nov-04 2:34
n4nilesh24-Nov-04 2:34 
GeneralRe: Project setup Pin
srirambalaji24-Nov-04 3:08
srirambalaji24-Nov-04 3:08 
GeneralRe: Project setup Pin
n4nilesh24-Nov-04 6:10
n4nilesh24-Nov-04 6:10 

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

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