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.
OnConnection
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
OnDisconnection
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
OnAddInsUpdate
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