LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Building an Office COM Add-in Using Visual Studio .NET
by Sumit Dhal
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 26761/ 56


Microsoft Office 2000 and later support new, uniform design architecture for building application add-ins to enhance and to control Office applications.  These add-ins are called COM add-ins.

In this step-by-step article we will discusses Office COM add-ins and describes how to build an Office COM add-in by using Microsoft Visual Studio .NET.

In this article we will also discuss the five methods provided by the IDTExtensibility2 interface.  All COM add-ins inherit from this interface and must implement each of its five methods.

Step-by-step example with Visual Studio .NET 2003

Step 1

On the File menu in Microsoft Visual Studio .NET, click New and then click Project.  In the New Project dialog box, expand Other Projects under Project Types, select Extensibility Projects, and then select the Shared Add-in template.  Type AddinDemo as the name of the add-in and then click OK.

Figure 1

Step 2

Now, the Extensibility Wizard appears as below.

Figure 2

Step 3

On page 1 select the preferred programming language and move Next.  For the purpose of our discussion Visual Basic is chosen.

Figure 3

Step 4

On page 2 select the host applications and then click Next. Here, Outlook is chosen.

Figure 4

Step 5

On page 3 provide a name and description for the add-in and then click Next.  The name and description of the add-in appear in the COM Add-in dialog box in the Office application.

Figure 5

Step 6

On page 4 select the options, specific for your development machine.

Figure 6

Step 7

Review the summary and click Finish to create the add-in.

Figure 7

IDTExtensibility2 Interface Implementation

The above steps will open up a code window with the default class Connect that implements Extensibility.IDTExtensibility2 interface.

A COM add-in is an in-process COM server, or ActiveX Dynamic Link Library (DLL), that implements the IDTExensibility2 interface as described in the Microsoft Add-in Designer type library (Msaddndr.dll).  All COM add-ins inherit from this interface and must implement each of its five methods. The methods are the following.


The OnConnection event fires whenever the COM add-in is connected.  The add-in may be connected on startup, by the end user, or through Automation.  If OnConnection returns successfully, the add-in is said to be loaded.  If an error message is returned, the host application immediately releases its reference to the add-in and the object is destroyed.

Within this method you can see two pre-written line of codes.

Listing 1

applicationObject = application
addInInstance = addInInst

From here the applicationObject holds the reference to the parent application (Word, Outlook, Excel ...) and the addInInstance holds the reference to the instance of the Add-In.  So at any time if you have the hold on these objects, their properties and methods, those can be intelligently programmed.

Listing 2

Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
      applicationObject = application
      addInInstance = addInInst
End Sub


The OnDisconnection event fires when the COM add-in is disconnected and just before it unloads from memory.  The add-in should perform any cleanup of resources in this event and should restore any changes made to the host application.

Listing 3

Public Sub OnDisconnection(ByVal RemoveMode As    
      Extensibility.ext_DisconnectMode, ByRef custom As System.Array)    
      Implements Extensibility.IDTExtensibility2.OnDisconnection
End Sub


The OnAddInsUpdate event fires when the set of registered COM add-ins changes.  In other words, whenever a COM add-in is installed or removed from the host application, this event fires.

Listing 4

Public Sub OnAddInsUpdate(ByRef custom As System.Array) 
      Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

OnStartupComplete and OnBeginShutdown

The OnStartupComplete and the OnBeginShutdown methods are called respectively when the host application is entering or leaving.  At both of these stages user interaction should be avoided because the application is busy loading or unloading itself from memory.  OnStartupComplete is only called if the add-in was connected during startup and OnBeginShutdown is only called if the host disconnects the add-in during shutdown.

Listing 5

Public Sub OnStartupComplete(ByRef custom As System.Array)
      Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub

Listing 6

Public Sub OnBeginShutdown(ByRef custom As System.Array)
      Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub

Creating COM add-ins to the Office application is made easy by the .NET architecture and Visual Studio .NET.  You can design a simple add-in to the Office application in less than 10 minutes. These add-ins can add a lot of new features to the basic Office application and make them more productive.

Sumit Kumar Dhal

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-01 6:43:56 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search