Building an Office COM Add-in Using Visual Studio .NET
page 2 of 4
by Sumit Dhal
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25552/ 42

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.

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

View Entire Article

User Comments

Title: Re: Testing, installing and uninstalling   
Name: Sumit
Date: 2008-12-13 12:37:47 PM
Comment:
Hello Bart,

To start Outlook/ any office app instead:
In the Project Properties - Debug - Start Action, choose Start External program and locate to your Office Installation Folder - Outlook exe.

The add in application must have created the associated installer(setup) project. Build this to create the msi file to be installed. Right click on the installer project in VS will directly give you install/ uninstall option. Note: Build the setup project appropiately before install to get the installer with latest code change.

You can also change the project output path of the addin application to point to your installation folder, from bin(default) to get the immediate output to take effect in the plugin.

Hope all this will help.

Thanks
Sumit
Title: Testing, installing and uninstalling   
Name: Bart
Date: 2008-12-11 1:24:41 PM
Comment:
Hello,

How can you test this add-in? If I run it, another session of the Visual Studio starts. Why?

How can we install and uninstall this add-in?

Thanks
Title: Replying Garg   
Name: saranya
Date: 2008-05-23 4:02:24 AM
Comment:
Can we see design view of the addins created
Title: Replying Garg   
Name: Sumit (Author)
Date: 2007-07-04 2:19:08 AM
Comment:
Hi Garg,

Please check the reference of the microsoft article (http://support.microsoft.com/kb/302896) in the reference section of my article.
I tried to make it more presentable, removing complecated stuffs, to encourage new developers to jump into this area.

I am working on Outlook com add-ins for about last two years. I will try to add few more useful stuffs in future article if I plan to. You can find few of my posts at www.outlookcode.com of Sue Mosher, that is the best site if one person is intrested in outlook COM programming.

thanks
- sumit
Title: This is just a copy of article from microsoft   
Name: GARG
Date: 2007-07-03 3:21:26 PM
Comment:
Please add some value to the tutorial. This is a copy of article from microsoft.
Title: To access a menuitem in Microsoft Office Infopath form   
Name: akjal
Date: 2007-03-14 11:00:43 AM
Comment:
Hi Sumith,
Thanks for such a simple and informative narration.

I have a requirement. In a Microsoft Infopath form , I need to access a MenuItem and click it through visual studio code.Could you pls suggest any solution.Could add-in work in this scenario?

Thanks in advance...
Title: For Prashant Malik   
Name: Sumit Kumer Dhal
Date: 2007-02-10 6:51:24 AM
Comment:
Hi Prashant,

It is easily doable.
But please let me know your requirement clearely. My mail id is dhalsumit@yahoo.co.in
drop me a mail (or to know your id) as I have no other way to reach you.

thanks
sumit
Title: office add in to write into the word document   
Name: prashant malik
Date: 2007-02-10 5:49:37 AM
Comment:
Hi
You have done a great job!!!

Now can you create or modify the existing add in to write something inside the word document. My requirement is like this..
add a text box instead of button and when a user write something inside
the text box it should display inside the word document(editor)

thanks in advance
Title: visual studio .net 2003 codeing books   
Name: saeed hussian
Date: 2006-11-30 10:13:30 AM
Comment:
pls send the books in visual basic 6 and visual studio .net 2003

thanks
Title: Informative one   
Name: sarit
Date: 2006-11-13 1:01:46 AM
Comment:
Let this flow going on.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 4:54:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search