How to write Outlook add-in / plugin in VB.NET, C#, C++.
Create ribbon tabs, menus, buttons for Outlook toolbar,

Add-in Express
for Microsoft .net


Add-in Express Home > Add-in Express.NET > Online Guide > Writing Outlook COM add-ins

Outlook add-ins in C#, C++, VB.NET

Creating add-in / plug-in for Outlook 2007 in .NET - Flash video All Microsoft Outlook COM add-ins / plug-ins provide two capabilities that developers request most often - extending menus and toolbars at the Explorer and Inspector levels, and extending property pages for folders and the main Options page. That is why Add-in Express provides several Outlook specific classes that allow you to use these features. Also, Add-in Express offers you some unique options for customizing the Navigation pane, Preview pane and Outlook bar. Find more about special features for extending the Outlook plug-in GUI.

Add-in Express .NET provides two Outlook specific toolbars (command bars): ADXOlExplorerCommandBar and ADXOlInspectorCommandBar. The first adds a toolbar to the Outlook Explorer window and solves many problems with custom Outlook command bars. The latter adds a toolbar to the Outlook Inspector window. Both ADXOlExplorerCommandBar and ADXOlInspectorCommandBar have the FolderName(s) and ItemTypes properties that add context-sensitivity to Outlook command bars. The OlExplorerItemTypes, OlInspectorItemTypes, and OlItemTypeAction properties add context-sensitivity to Outlook toolbar controls.

There are also two Outlook-specific main menu components: ADXOlExplorerMainMenu and ADXOlInspectorMainMenu. Additionally, Add-in Express supplies the Outlook Property Page component that helps you to add your pages to the Options (Tools | Options menu) and Folder Properties dialogs.

Example of creating an Outlook COM add-in

The example below shows you step-by-step how to write a COM add-in for Outlook 2000 - 2007. It is written in VB.NET, but you can develop your COM add-in projects in C++, C# and RemObjects Chrome as well. You may also want to see another sample Outlook 2007 add-in project in VB.NET and download a free Outlook add-in in C#, VB.NET and C++.

Step 1. Creating a new Outlook COM add-in project

To create a new COM add-in project close all opened solutions, choose "File | New | Project...", select the "Other Projects | Extensibility Projects" item, and choose the Add-in Express COM Add-in project template in the Visual Studio IDE.

Create a new Outlook add-in project
 
This will start the Add-in Express .NET wizard. In the wizard windows, you choose the programming language and setup project options of your add-in. In our sample project it is VB.NET. But you can also develop in C# and C++.

COM add-in project wizard
 
When you click the Next button, the following window appears. Here you choose the host applications supported by your add-in. Since this sample add-in is for Outlook, so our choice is evident.

This VB.NET sample shows a project implementing an Outlook COM add-in with the Add-in Express Loader.

Selecting Outlook as a supported application
 
Additionally, Add-in Express provides you with version-neutral PIAs (Primary Interop Assemblies). The version-neutral PIAs allow creating add-ins / plugins working with all Outlook versions: from 2000 to 2007.

When you click the Finish button, the Add-in Express Project wizard creates and opens the COM add-in solution in Visual Studio. The solution includes the COM Add-in project and the setup project for your Outlook add-in.

Outlook add-in solution
 
The COM Add-in project contains the AddinModule.vb (or AddinModule1.cs) file described in the next step.

Step 2. COM Add-in module

The AddinModule.vb (or AddinModule1.cs) is a COM Add-in module that is the core part of the COM add-in project. The add-in module implements the IDTExtensibility2 interface and allows you to manage add-in toolbars and their controls. The module is the container for Add-in Express components, which allow you to concentrate on the functionality of your add-in. You specify add-in properties in the module's properties, add components to the module's designer, and write the functional code of your add-in in this module. To review its source code, in Solution Explorer, right-click the AddinModule1.vb (or AddinModule1.cs) file and choose the View Code popup menu item.

Outlook add-in module code

The code for AddinModule1.vb follows below. Please, pay attention to the OutlookApp property of the module generated by the Add-in Express Project wizard. You can use it in your code to get access to Outlook objects.


Imports System.Runtime.InteropServices
Imports System.ComponentModel
'Add-in Express Add-in Module
<GuidAttribute("3BDF26A5-74E4-42CB-A93A-E88435BC0AD3"), _
      ProgIdAttribute("MyOutlookAddin1.AddinModule")> _
Public Class AddinModule
   Inherits AddinExpress.MSO.ADXAddinModule
 
#Region " Component Designer generated code. "
   'Required by designer
   Private components As System.ComponentModel.IContainer
   'Required by designer - do not modify
   'the following method
   Private Sub InitializeComponent()
      Me.components = New System.ComponentModel.Container
      '
      'AddinModule
      '
      Me.AddinName = "MyOutlookAddin1"
      Me.SupportedApps = AddinExpress.MSO.ADXOfficeHostApp.ohaOutlook
   End Sub
#End Region
#Region " Add-in Express automatic code "
   'Required by Add-in Express - do not modify
   'the methods within this region
   Public Overrides Function GetContainer() As _
         System.ComponentModel.IContainer
      If components Is Nothing Then
         components = New System.ComponentModel.Container
      End If
      GetContainer = components
   End Function
   <ComRegisterFunctionAttribute()> _
   Public Shared Sub AddinRegister(ByVal t As Type)
      AddinExpress.MSO.ADXAddinModule.ADXRegister(t)
   End Sub
   <ComUnregisterFunctionAttribute()> _
   Public Shared Sub AddinUnregister(ByVal t As Type)
      AddinExpress.MSO.ADXAddinModule.ADXUnregister(t)
   End Sub
   Public Overrides Sub UninstallControls()
      MyBase.UninstallControls()
   End Sub
#End Region
   Public Sub New()
      MyBase.New()
      'This call is required by the Component Designer
      InitializeComponent()
      'Add any initialization after the InitializeComponent() call
   End Sub
   Public ReadOnly Property OutlookApp() As Outlook._Application
      Get
         Return CType(HostApplication, Outlook._Application)
      End Get
   End Property
End Class

Step 3. Customizing your Outlook add-in

The Add-in Express COM Add-in designer allows setting add-in properties and adding components to the module. In the Solution Explorer window, open the add-in module in the design view, and activate the Properties window. In the Properties window, you set the name and description of your add-in module (see COM add-in designers).

Add-in module properties
 
To add an Add-in Express component to the module, you use an appropriate command in the Properties window, or you can right-click the designer surface and choose the same command in the context menu.

You can use the following module commands in Outlook add-ins:

  • Add Explorer CommandBar – adds an Outlook Explorer toolbar to your add-in.
  • Add Inspector CommandBar – adds an Outlook Inspector toolbar to your add-in.
  • Add Outlook Bar Shortcut Manager – adds a component that allows adding Outlook Bar shortcuts and shortcut groups (see Outlook Bar Shortcut Manager).
  • Add Outlook Forms Manager – adds a component that allows embedding custom .NET forms into Outlook windows (see Outlook Forms Manager).
  • Add Ribbon Tab – adds a Ribbon tab to your add-in (see Office 2007 Ribbon components).
  • Add Ribbon Quick Access Toolbar – adds a component that allows customizing the Ribbon Quick Access Toolbar in your add-in.
  • Add Ribbon Office Menu – adds a component that allows customizing the Ribbon Office Menu in your add-in.
  • Add Events – adds or deletes components that provide access to Outlook application-level events (see Application-level events).

See more commands of the add-in module.

Step 4. Adding a new Explorer toolbar

Add-in Express provides two Outlook specific command bars: ADXOlExplorerCommandBar and ADXOlInspectorCommandBar.

Adding a new Outlook Explorer toolbar

To add a command bar to the Outlook Explorer window, use the Add Explorer CommandBar command that adds an ADXOlExplorerCommandBar component to the add-in module.

Outlook Explorer toolbar properties
 
Select the Explorer CommandBar component, and, in the Properties window, specify the toolbar name using the CommandBarName property and choose its position (see the Position property on the screenshot above). Outlook-specific versions of Add-in Express CommandBar component provides context-sensitive properties. They are FolderName, FolderNames, and ItemTypes (see COM add-ins for Outlook – template characters in FolderName).

In the screenshot above, you see the Outlook Explorer toolbar that will be shown for every Outlook folder (FolderName = "*") the default item types of which are Mail or Task. See Command bars: toolbars, menus, context menus).

Step 5. Adding a new toolbar button

To add a new button to the Explorer command bar, in the Properties window, you select the Controls property and click the property editor button (the button in the property value box). This runs the visual designer. Use its toolbar to add or remove Command bar controls. Just click the appropriate button and see the result.

Adding a new command bar  button.

Specify the button's Caption property, set the Style property (default value = adxMsoButtonCaption), and close the collection editor. To handle the Click event of the button, select the added button in the topmost combo of the Properties window and add the Click event handler. The code of the event handler follows below (it is empty as you can see):


    Private Sub AdxCommandBarButton1_Click(ByVal sender As System.Object) _
           Handles AdxCommandBarButton1.Click
    
    End Sub

For more details about extending Outlook command bar with custom toolbar controls, see a sample project that shows how to add visual .NET controls to Outlook toolbar.

Step 6. Accessing Outlook objects

The Add-in Module provides the HostApplication property that returns the Application object (of the Object type) of the host application the add-in is currently running in. For your convenience, the ADD-IN EXPRESS Project wizard adds host-related properties to the Add-in module. You use these properties to access host application objects. For instance, this sample add-in includes the OutlookApp property in the Add-in module:


   Public ReadOnly Property OutlookApp() As Outlook._Application
      Get
         Return CType(HostApplication, Outlook._Application)
      End Get
   End Property

The _Application object provides the same properties and methods as the Application object but it doesn't provide events. This allows you to write the following code to the Click event of the button just added:


   Private Sub AdxCommandBarButton1_Click(ByVal sender As System.Object) _
         Handles AdxCommandBarButton1.Click
      MsgBox("The subject is:" _
         + vbCrLf _
         + Me.OutlookApp.ActiveExplorer.Selection.Item(1).Subject)
   End Sub

Step 7. Handling Outlook events

The COM add-in designer provides the Add Events command that adds (and remove) event components that allow handling application-level events. When you choose this command, the Add Application Events dialog box opens allowing you to select the application events components you need. In this sample, we add the Outlook Events component to the add-in module.

With the Outlook Events component, you handle any application-level events of Outlook. For instance, the following code handles the BeforeFolderSwitch event of the Outlook Explorer class:

Adding Outlook Events component

With the Outlook Events component, you handle any application-level events of Outlook. For instance, the following code handles the BeforeFolderSwitch event of the Outlook Explorer class:


   Private Sub adxOutlookEvents_ExplorerBeforeFolderSwitch _
      (ByVal sender As Object, _
      ByVal e As _ 
             AddinExpress.MSO.ADXOlExplorerBeforeFolderSwitchEventArgs) _
      Handles adxOutlookEvents.ExplorerBeforeFolderSwitch
      MsgBox("You are switching to the " + e.NewFolder.Name + " folder")
   End Sub

To process events of the Folders and Items classes as well as all item sorts in Outlook, see Event classes.

Step 8. Adding a new Inspector toolbar

To add a toolbar to the Outlook Inspector window, use the Add Inspector CommandBar command that adds an ADXOlInspectorCommandBar component to the COM Add-in module.

Outlook Inspector toolbar component

The Inspector toolbar component provides the same properties as the Explorer command bar component. In the screenshot below you see the default values for the Inspector toolbar.

Outlook Inspector command bar component properties

Add a new button to the toolbar. Now you can show the subject of the currently open mail item using the following code that handles the Click event of the button:


   Private Sub AdxCommandBarButton2_Click(ByVal sender As System.Object) _
            Handles AdxCommandBarButton2.Click
      MsgBox("The subject is:" _
         + vbCrLf _
         + Me.OutlookApp.ActiveInspector.CurrentItem.Subject)
   End Sub

See also Customizing Outlook Inspector window.

Step 9. Customizing Outlook main menu

Outlook provides two main menu types. They are available for two main types of Outlook windows: Explorer and Inspector. Accordingly, Add-in Express provides two main components: Explorer Main Menu component and Inspector Main Menu component. You add either of them using the context menu of the add-in module. Then you use the visual designer provided for the Controls property of the component. For instance, to add a custom control to the popup shown by the File | New item in all Outlook Explorer windows, you do the following:

1. Use our free Built-in Control Scanner to scan the command bars and controls of Outlook

The screenshot below shows the result of scanning. You will need Office IDs from the screenshot to bind Add-in Express controls to them:

Scanned Office IDs
 
2. Add a popup control to the menu and set its Id property to 30002
3. Add a popup control to the popup control above and set its Id to 30037
4. Add a button to the popup above and specify its properties.

In the sample add-in described in this chapter, the BeforeId property of the My Item button is set to 1757 which is the ID of the Mail Message item. In this way, we show the custom item before the Mail Message one.

The following screenshot shows the settings of the popup created at step 3 above:

Outlook pop-up control settings

Step 10. Customizing Outlook context menu

Outlook allows customizing several context menus. Add-in Express allows customizing Outlook menus via the Context Menu component. You use the context menu of the add-in module to add such a component onto the module. Then you choose Outlook in the SupportApp property of the component. Then, in the CommanBarName property, you choose the context menu you want to customize. And finally, you add custom controls in the visual designer supplied for the Controls property.

The sample add-in described in this chapter adds a custom item to the Folder Context Menu command bar that implements the context menu which is shown when you right-click a folder in the folder tree

Step 11. Handling events of Outlook Items object

The Outlook MAPIFolder class provides the Items collection. This collection provides the following events: ItemAdd, ItemChange, and ItemRemove. To process these events, you use the Outlook Items Events Class located in the Add-in Express Items folder in the Add New Item dialog:

Adding Outlook Items Event class
 
This will add the OutlookItemsEventsClass1.vb class to the add-in project. You handle the ItemAdd event by entering some code into the ProcessItemAdd procedure of the class:


Imports System
 
'Add-in Express Outlook Items Events Class
Public Class OutlookItemsEventsClass1
   Inherits AddinExpress.MSO.ADXOutlookItemsEvents

   Public Sub New(ByVal ADXModule As AddinExpress.MSO.ADXAddinModule)
      MyBase.New(ADXModule)
   End Sub

   Public Overrides Sub ProcessItemAdd(ByVal Item As Object)
      MsgBox("The item with subject '" + Item.Subject + _
         "' has been added to the Inbox folder")
   End Sub

   Public Overrides Sub ProcessItemChange(ByVal Item As Object)
      'TODO: Add some code
   End Sub

   Public Overrides Sub ProcessItemRemove()
      'TODO: Add some code
   End Sub
End Class

This requires you to add the following declarations and code to the Add-in Module:


   Dim ItemsEvents As OutlookItemsEventsClass1 = _
          New OutlookItemsEventsClass1(Me)
...
   Private Sub AddinModule_AddinBeginShutdown(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Me.AddinBeginShutdown
      If ItemsEvents IsNot Nothing Then
         ItemsEvents.RemoveConnection()
         ItemsEvents = Nothing
      End If
   End Sub

   Private Sub AddinModule_AddinStartupComplete(ByVal sender As Object, _ 
         ByVal e As System.EventArgs) Handles Me.AddinStartupComplete
      ItemsEvents.ConnectTo( _
            AddinExpress.MSO.ADXOlDefaultFolders.olFolderInbox, True)
   End Sub

Step 12. Adding Outlook folder property pages

Outlook allows you to add custom option pages to the Options dialog box (the Tools | Options menu) and / or to the Properties dialog box of any folder. To automate this task, Add-in Express provides the ADXOlPropertyPage component. You find it in the Add New Item dialog box.

Adding Outlook property page
 
Click the Add button to add a new property page instance, a descendant of the ADXOlPropertyPage class that implements the IPropertyPage interface:


Imports System.Runtime.InteropServices
 
'Add-in Express Outlook Option Page
<GuidAttribute("D8F61EE3-B676-4FFA-9CAF-BAC46C1F0934"), _
       ProgIdAttribute("PropertyPage1.OutlookPage")> _
Public Class PropertyPage1
    Inherits AddinExpress.MSO.ADXOlPropertyPage
 
#Region " Component Designer generated code "
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Component Designer
        InitializeComponent()
 
        'Add any initialization after the InitializeComponent() call
    End Sub
 
   'Clean up any resources being used
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub
 
    'Required by designer
    Private components As System.ComponentModel.IContainer
 
    'Required by designer - do not modify
    'the following method
    <System.Diagnostics.DebuggerStepThrough()> _
       Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
        '
        'PropertyPage1
        '
        Me.Name = "PropertyPage1"
        Me.Size = New System.Drawing.Size(413, 358)
    End Sub
#End Region
End Class

You can customize the page as an ordinary form: add the controls and handle their events. To add a property page to the <FolderName> Properties dialog box of an Outlook folder(s), you do the following:

  • In the AddinModule properties, run the editor of the FolderPages property.
  • Click the Add button.
  • Specify the folder you need in the FolderName property.
  • Set the PageType property to the PropertyPage component you've added.
  • Specify the Title property and close the dialog box.

Outlook property page properties

The screenshot above shows the settings you need to display your page in the Folder Properties dialog for all Mail folders (ItemTypes = Mail). In order to limit the property page to the Inbox folder only, change the code of the AddinStartupComplete event in the AddinModule as follows:


    Private Sub AddinModule_AddinStartupComplete(ByVal sender As Object, _
             ByVal e As System.EventArgs) Handles Me.AddinStartupComplete
        ItemsEvents.ConnectTo( _
           AddinExpress.MSO.ADXOlDefaultFolders.olFolderInbox, True)
        Dim FullPath As String = _
           GetFolderPath(Me.OutlookApp.GetNamespace("Mapi"). _
              GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox))
        ' remove leading backslashes
        Me.FolderPages.Item(0).FolderName = _
           FullPath.Substring(2, FullPath.Length - 2)
    End Sub

Note, see the code of the GetFolderPath function here: FolderPath property value in Outlook 2000 and XP.

In order to control the events for the folder, add a checkbox to the page and handle its CheckedChanged event as well as the Dirty, Apply, and Load events of the page as follows:


...
   Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
   Private TrackStatusChanges As Boolean

   ...

   Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
      If Not TrackStatusChanges Then Me.OnStatusChange()
   End Sub

   Private Sub PropertyPage1_Dirty(ByVal sender As System.Object, _
         ByVal e As AddinExpress.MSO.ADXDirtyEventArgs) Handles MyBase.Dirty
      e.Dirty = True
   End Sub

   Private Sub PropertyPage1_Apply(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles MyBase.Apply
      CType(AddinModule.CurrentInstance, _
         MyOutlookAddin1.AddinModule).IsFolderTracked = _
            Me.CheckBox1.Checked
   End Sub

   Private Sub PropertyPage1_Load(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Me.Load
      TrackStatusChanges = True
      Me.CheckBox1.Checked = _
            CType(AddinModule.CurrentInstance, _
            MyOutlookAddin1.AddinModule).IsFolderTracked
      TrackStatusChanges = False
   End Sub
End Class

Finally, you add the following property to the AddinModule code:


   ...
   Friend Property IsFolderTracked() As Boolean
      Get
         Return ItemsEvents.IsConnected
      End Get
      Set(ByVal value As Boolean)
         If value Then
            ItemsEvents.ConnectTo(ADXOlDefaultFolders.olFolderInbox, True)
         Else
            ItemsEvents.RemoveConnection()
         End If
      End Set
   End Property

Adding a page to the Outlook Options dialog

To add this or other property page to the main Options dialog box, you use the PageType and PageTitle properties of the add-in module.

PageProgId property is obsolete

Note, whether you add a folder property page or an Outlook Options page, do not use the PageProgId property. It is obsolete; we left it for compatibility only. See also Outlook Property page.

Step 13. Intercepting keyboard shortcuts

To intercept a keyboard shortcut, you add an ADXKeyboardShortcut component to the COM Add-in Module using the Add Keyboard Shortcut command of the module.

Keyboard Shortcut component
 
In the Properties window you select (or enter) the desired shortcut in the ShortcutText property. We chose the shortcut for the Send button in the mail Inspector's Standard command bar. It is Ctrl+Enter.

HandleShortcuts property

To use keyboard shortcuts, you need to set the HandleShortcuts property of AddinModule to true.

 Keyboard Shortcut component properties

Now you handle the Action event of the component:


   Private Sub AdxKeyboardShortcut1_Action(ByVal sender As System.Object) _
      Handles AdxKeyboardShortcut1.Action
      MsgBox("You've pressed " + _
         CType(sender, AddinExpress.MSO.ADXKeyboardShortcut).ShortcutText)
   End Sub

Step 14. Customizing the Outlook 2007 Ribbon user interface

To add a new tab to the Ribbon, you use the Add Ribbon Tab command that adds an ADXRibbonTab component to the module.

Add-in a new tab to the Outlook 2007 Ribbon
 
In the Properties window, run the editor for the Controls collection of the tab. In the designer window, use the toolbar buttons or context menu to add or delete Add-in Express components that form the Ribbon interface of your add-in. First, you add a Ribbon tab and change its caption to My Ribbon Tab. Then, you select the tab component, add a Ribbon group, and change its caption to My Ribbon Group. Next, you select the group, and add a button group. Finally, you select the button group and add a button. Set the button caption to My Ribbon Button. Use the ImageList and Image properties to set the icon for the button.

Adding a new Ribbon button
 
Click OK, and, in the Properties window, find the newly added Ribbon button. Now add the event handler to the Click event of the button. Write the following code:

    Private Sub AdxRibbonButton1_OnClick(ByVal sender As System.Object, _
        ByVal control As AddinExpress.MSO.IRibbonControl, _
        ByVal pressed As System.Boolean) Handles AdxRibbonButton1.OnClick
        AdxCommandBarButton2_Click(Nothing)
    End Sub

Remember, the Office 2007 Ribbon tab designer performs the XML-schema validation automatically, so from time to time you will run into the situation when you cannot add a control to some level. It is a restriction of the Ribbon XML-schema.

Note, unlike other Ribbon-based applications Outlook, has numerous ribbons. Please use the Ribbons property of your ADXRibbonTab components to specify the ribbons you customize with your tabs. See also Office 2007 ribbon components and tips about Outlook 2007 ribbons.

Step 15. Adding a custom task pane in Outlook 2007

To add a custom task pane in Outlook 2000-2003, please see the customizing Outlook form page.

To add a new task pane to your Outlook 2007 project, you add a UserControl and some controls. Then you add an item to the TaskPanes collection of the Add-in Module and specify its properties:

  • Caption – the caption of your task pane.
  • Height, Width – the height and width of your task pane (applies to horizontal and vertical task panes, correspondingly ).
  • DockPosition – you can dock your task pane to the left, top, right, or bottom edges of the host application window.
  • ControlProgID – the UserControl just added.

The sample Outlook add-in described here has the following task pane settings:

Task pane settings
 
In Add-in Express, you work with the task pane component and task pane instances. The TaskPanes collection of the add-in module contains task pane components. When you set, say, the height or dock position of the component, these properties apply to every task pane instance that the host application shows. To modify a property of a task pane instance, you should get the instance itself. This can be done through the Item property of the component (in C# this property is the indexer for the ADXTaskPane class). The property accepts the Outlook Explorer or Inspector window object that displays the task pane as a parameter. For instance, the RefreshTaskPane method in the samples' add-in module finds the currently active instance of the task pane and refreshes it. For the task pane to be refreshed in a consistent manner, this method is called in appropriate event handlers.


Private Sub RefreshTaskPane(ByVal ExplorerOrInspector As Object)
      If Me.HostVersion.Substring(0, 4) = "12.0" Then
         Dim TaskPaneInstance As _
            AddinExpress.MSO.ADXTaskPane.ADXCustomTaskPaneInstance = _
               AdxTaskPane1.Item(ExplorerOrInspector)
         If Not TaskPaneInstance Is Nothing _
            And TaskPaneInstance.Visible Then
            Dim uc As UserControl1 = TaskPaneInstance.Control
            If Not uc Is Nothing Then _
              uc.InfoString = GetSubject(ExplorerOrInspector)
         End If
      End If
   End Sub

The InfoString property just gets or sets the text of the Label located on the UserControl1. The GetSubject method is shown below.


Private Function GetSubject(ByVal ExplorerOrInspector As Object) _
      As String
      Dim mailItem As Outlook.MailItem = Nothing
      Dim selection As Outlook.Selection = Nothing

      If TypeOf ExplorerOrInspector Is Outlook.Explorer Then
         Try
            selection = CType(ExplorerOrInspector, _
               Outlook.Explorer).Selection
            mailItem = selection.Item(1)
         Catch
         Finally
            If Not selection Is Nothing Then _
               Marshal.ReleaseComObject(selection)
         End Try
      ElseIf TypeOf ExplorerOrInspector Is Outlook.Inspector Then
         Try
            mailItem = CType(ExplorerOrInspector, _
               Outlook.Inspector).CurrentItem
         Catch
         End Try
      End If

      If mailItem Is Nothing Then
         Return ""
      Else
         Dim subject As String = "The subject is: " + mailItem.Subject
         Marshal.ReleaseComObject(mailItem)
         Return subject
      End If
   End Function

The code of the RefreshMe method emphasizes the following:

  • The OutlookWindow parameter was originally obtained through parameters of Add-in Express event handlers. That's why we don't release it. See also Releasing COM objects.
  • The CurrentSelection and CurrentMail variables were obtained "manually" so they must be released.
  • Outlook 2007 (at least) fires an exception when you try to obtain the Selection object in some situations.
  • There can be no items in the Selection object.

You may also want to look at the event handler for the VisibleStateChange event of task pane component in the code of the sample add-in described on this page. It works with the CustomTaskPane interface defined in Office 2007.

If you develop for Excel, see also how to create a custom Excel task pane.

Step 16. Running the Outlook COM add-in

Save the add-in project, close Outlook, build and register the plugin using the Register Add-in Express project item from the Build menu, then restart Outlook and find your option page(s), command bars, and controls, Ribbon controls, and custom task panes:

Running Outlook COM add-in
 
You can find your add-in in the COM Add-ins dialog. See also Add the COM add-ins command to a toolbar or menu.

Step 17. Debugging the Outlook COM add-in

To debug your add-in, just indicate the add-in host application as the Start Program in the Project Options window.

Debugging Outlook add-in project
 
However, when debugging an add-in or a smart tag on Visual Studio 2003 and Office XP you cannot see your add-in or smart tag on the COM add-ins or AutoCorrect dialog box. This is a “feature” of Office XP "added" by Microsoft.

Step 18. Deploying your Outlook COM add-in

Just built the setup project, copy all setup files to the target PC and run the setup.exe file to install the add-in. Read more about deploying Outlook COM add-in,  MSI and ClickOnce deployment,  find some useful deploying and debugging tips.

Building Office COM add-in <<

>> Customizing Outlook forms

Back to Add-in Express.NET homepage




Client login

 

Login 

Password 

 

Remember me

Forgot my password