Creating and Handling CommandBars in multiple Inspectors of Outlook

The window which displays a single item (email/appointment/task) in the Outlook is called as an Inspector in Outlook. It is derived from the namespace Microsoft.Office.Outlook.Inspector. Inspector class has methods  and properties using which we can do modifications to the window and handle the events that are raised.
We can get Inspector object by using one approach from the following –

  • Use the Inspectors property of the Microsoft.Office.Tools.Outlook.Application class to access all of the Inspector objects in Outlook.

(or)

  • Use the GetInspector method of a specific item, such as a Microsoft.Office.Interop.Outlook.MailItem or Microsoft.Office.Interop.Outlook.AppointmentItem, to retrieve the Inspector associated with it.

(or)

  • Use the ActiveInspector method of the Microsoft.Office.Tools.Outlook.Application class to get the Inspector that currently has focus.

Now, lets see how to create a CommandBar with some buttons and handling the events of them.
Following code has been written by Sue Mosher
ThisApplication.vb  (standard module generated for VSTO 2005 Outook add-in project)

Public Class ThisApplication
Dim WithEvents allInsp As Outlook.Inspectors
Public allInspHelper As New InspectorHelperClass
Private Sub ThisApplication_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
      allInsp = Globals.ThisApplication.Inspectors
End Sub
Private Sub ThisApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
      allInspHelper = Nothing
      allInsp = Nothing
End Sub
Private Sub allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles allInsp.NewInspector
      Dim itm As Object
      Dim id As String
      On Error Resume Next
      ' for the item types you want to have your CommandBar or button
      ' add the Inspector to the wrapper class collection
      itm = Inspector.CurrentItem
      If TypeOf itm Is Outlook.MailItem Then
            id = allInspHelper.AddInsp(Inspector)
      ElseIf TypeOf itm Is Outlook.ContactItem Then
            id = allInspHelper.AddInsp(Inspector)
      End If
      itm = Nothing
End Sub
End Class

InspectorHelperClass.vb

Imports Microsoft.Office.Interop.Outlook
Public Class InspectorHelperClass
 Public Shared InspectorsList As New InspectorWrapperCollection
 Private intID As Integer
 Public Function AddInsp(ByVal Inspector As Outlook.Inspector) As String
 Dim iWrap As New InspectorWrapper()
 Dim itm As Object
 Dim id As String
 On Error Resume Next
 'set the Inspector in the class
 iWrap.Inspector = Inspector
 'test which Outlook item type is here
 itm = Inspector.CurrentItem
 If TypeOf itm Is Outlook.MailItem Then
 iWrap.MailItem = itm
 ElseIf TypeOf itm Is Outlook.ContactItem Then
 iWrap.ContactItem = itm
 End If
 itm = Nothing
 iWrap.Key = intID
 id = CStr(intID)
 'add the class to the collection with a
 ' unique Key value.
 InspectorsList.Add(iWrap, id)
 'create buttons and menus for the Inspector
 iWrap.InitButton()
 AddInsp = id
 intID = intID + 1
 iWrap = Nothing
 itm = Nothing
 End Function
 Public Sub KillInsp(ByVal intID As Integer, ByVal inspWrap As InspectorWrapper)
 Dim inspWrap2 As InspectorWrapper
 On Error Resume Next
 inspWrap2 = InspectorsList.Item(CStr(intID))
 ' check to make sure we're removing the
 ' correct Inspector from the collection.
 If Not inspWrap2 Is inspWrap Then
 Err.Raise(1, Description:="Unexpected Error in KillInsp")
 Else
 InspectorsList.Remove(CStr(intID))
 End If
 inspWrap2 = Nothing
 End Sub
End Class

InspectorWrapper.vb (This code is developed by Ken Slovak)

Imports Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Outlook
Public Class InspectorWrapperCollection
 Inherits DictionaryBase
 Public Overloads Sub Add(ByVal myWrap As InspectorWrapper, ByVal myKey As String)
 MyBase.Dictionary.Add(myKey, myWrap)
 End Sub
 Default Public Property item(ByVal theKey As String) As InspectorWrapper
 Get
 Return CType(MyBase.Dictionary.Item(theKey), InspectorWrapper)
 End Get
 Set(ByVal value As InspectorWrapper)
 MyBase.Dictionary(theKey) = value
 End Set
 End Property
 Public ReadOnly Property keys() As ICollection
 Get
 Return MyBase.Dictionary.Values
 End Get
 End Property
 Public Sub Remove(ByVal theKey As String)
 MyBase.Dictionary.Remove(theKey)
 End Sub
End Class
Public Class InspectorWrapper
 Private WithEvents _thisInsp As Outlook.Inspector
 Private WithEvents _thisMail As Outlook.MailItem
 Private WithEvents _thisContact As Outlook.ContactItem
 Private WithEvents _cbb As Office.CommandBarButton
 Private _obj As Object
 Private _intID As Integer
 Private _tag As String
 Private _hasMailInspector As Boolean
 Private _hasWordEditor As Boolean
 Private Sub _cbb_Click1(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) _
 Handles _cbb.Click
 Dim msgText As String
 On Error Resume Next
 'code here to handle a click of the menu/toolbar button
 ' and perform whatever function you want.
 msgText = "You clicked the button on the " & _
 _thisInsp.Caption & " window."
 MessageBox.Show(msgText, "Command Button Click Event")
 End Sub
 Public Sub New()
 On Error Resume Next
 _thisInsp = Nothing
 _thisMail = Nothing
 _thisContact = Nothing
 _cbb = Nothing
 _obj = Nothing
 _hasWordEditor = False
 End Sub
 Protected Overrides Sub Finalize()
 On Error Resume Next
 _thisInsp = Nothing
 _thisMail = Nothing
 _thisContact = Nothing
 _cbb = Nothing
 _obj = Nothing
 End Sub
 Public Function InitButton() As Boolean
 On Error Resume Next
 'if you want buttons only for
 ' certain item types you can test
 ' for that here using
 ' _thisInsp.CurrentItem.Class or TypeOf _thisInsp.CurrentItem
 Call CreateButtons(_thisInsp)
 'you can now enable/disable buttons depending
 ' on what type if item is opened if you want.
 End Function
#Region "InspectorWrapper class properties"
 Public WriteOnly Property MailItem() As Outlook.MailItem
 Set(ByVal value As Outlook.MailItem)
 _thisMail = value
 _hasMailInspector = True
 End Set
 End Property
 Public WriteOnly Property ContactItem() As Outlook.ContactItem
 Set(ByVal value As Outlook.ContactItem)
 _thisContact = value
 _hasMailInspector = False
 End Set
 End Property
 Public Property Inspector() As Outlook.Inspector
 Get
 Inspector = _thisInsp
 End Get
 Set(ByVal value As Outlook.Inspector)
 _thisInsp = value
 End Set
 End Property
 Public Property Key() As Long
 ' key is used to set unique tag for each CommandBarButton
 Get
 Key = _intID
 End Get
 Set(ByVal value As Long)
 _intID = value
 End Set
 End Property
#End Region
#Region "Open and Close event handlers for Outlook object properties"
 Private Sub _thisInsp_Close() Handles _thisInsp.Close
 On Error Resume Next
 Me.KillButtons()
 Globals.ThisApplication.allInspHelper.KillInsp(_intID, Me)
 _thisInsp = Nothing
 End Sub
 Private Sub _thisContact_Close(ByRef Cancel As Boolean) Handles _thisContact.Close
 On Error Resume Next
 'can handle various events for the contact item
 ' in the Inspector like Close and Open.
 If Cancel = False Then
 Me.KillButtons()
 Globals.ThisApplication.allInspHelper.KillInsp(_intID, Me)
 _thisInsp = Nothing
 End If
 End Sub
 Private Sub _thisContact_Open(ByRef Cancel As Boolean) Handles _thisContact.Open
 On Error Resume Next
 'can handle various events for the contact item
 ' in the Inspector like Close and Open.
 End Sub
 Private Sub _thisMail_Open(ByRef Cancel As Boolean) Handles _thisMail.Open
 On Error Resume Next
 'can handle various events for the mail item
 ' in the Inspector like Close and Open.
 End Sub
 Private Sub _thisMail_Close(ByRef Cancel As Boolean) Handles _thisMail.Close
 On Error Resume Next
 'can handle various events for the mail item
 ' in the Inspector like Close and Open.
 If Cancel = False Then
 Me.KillButtons()
 Globals.ThisApplication.allInspHelper.KillInsp(_intID, Me)
 _thisInsp = Nothing
 End If
 End Sub
#End Region
#Region "Private procedures to create and remove toolbars,menus, and buttons in the wrapped Inspector"
 Private Sub CreateButtons(ByVal myInsp As Outlook.Inspector)
 On Error Resume Next
 'Adding a new menu item and a button to the main menu for any Inspector
 ' must take a different approach if using Word as email editor.
 If (myInsp.IsWordMail = True) And _
 (myInsp.EditorType = Outlook.OlEditorType.olEditorWord) Then
 _hasWordEditor = True
 _obj = Nothing
 ' #### ADD CODE HERE TO ADD TOOLBAR TO WORD
 Else
 _hasWordEditor = False
 _obj = myInsp
 Me.CreateToolbar()
 End If
 Err.Clear()
 End Sub
 Private Sub CreateToolbar()
 'add a toolbar to the main Inspector  bar
 ' in the Inspector.
 Dim _tag As String
 Dim myToolBar As Office.CommandBar
 On Error Resume Next
 _tag = "This string is unique to this button" & CStr(_intID)
 'check for the toolbar existing already and do not create
 ' it if does exist. The checking code would set a
 ' Boolean variable named blnToolbarExists.
 myToolBar = _thisInsp.CommandBars("My Bar")
 If myToolBar Is Nothing Then
 myToolBar = _thisInsp.CommandBars.Add(Name:="My Bar", Temporary:=True)
 End If
 ' add the button to the toolbar
 _cbb = myToolBar.Controls.Add(Type:=Office.MsoControlType.msoControlButton, _
 Temporary:=True)
 With _cbb
 .Tag = _tag
 .Caption = "Test Button"
 .TooltipText = "The ToolTip for the button"
 End With
 myToolBar.Visible = True
 myToolBar = Nothing
 End Sub
 Private Sub KillButtons()
 Dim oControl As Office.CommandBarControl
 On Error Resume Next
 ' if you had multiple buttons, you'd need a different tag
 ' property for each one and would delete each one individually
 oControl = _obj.CommandBars.FindControl(Tag:=_tag)
 If Not oControl Is Nothing Then
 oControl.Delete()
 End If
 oControl = Nothing
 End Sub
#End Region
End Class

Code has been adapted from the following sites. You can find more information regarding this there. Keeping it here so that it helps someone who is in need of it.

  1. http://www.outlookcode.com/codedetail.aspx?id=1344
  2. http://www.slovaktech.com/code_samples.htm#InspectorWrapper
  3. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnhcvb04/html/vb04c10.asp

Developing add-ins for multiple versions of Office

Do you want to develop an add-in targeting multiple versions of Office? And you have basic questions like “Is it possible to do? ” and “How to do it?” ? Then you came to the right place. Few months back, I got a requirement to developed add-ins for Outlook 2003 and Outlook 2007. The functionality for both the versions is same. A doubt stroked… when the functionality is same, why would I develop two add-ins separately? Why don’t I make a single build for both the versions of Office?
Then I started searching for techniques to develop add-ins which works in both (2003 and 2007) and read many articles written by VSTO Experts in their blogs, Official VSTO Blog, MSDN, Forums and what not.
Misha Says:

Theoretically, you can develop an add-in for multiple versions of Microsoft Office by catering to the lowest common denominator. This means if you use an Excel 2003 add-in template in Visual Studio 2008, you would be able to develop and debug this with Excel 2007. However if you try this, you may meet these error messages: “You cannot debug or run this project, because the required version of the Microsoft Office application is not installed.”, followed by “Unable to start debugging.”

You can develop Office 2003 add-in in a system where Office 2007 is installed. The following is the procedure that demonstrates how to update your Visual Studio debugging options to use Microsoft Outlook 2007 to debug an add-in targeting Microsoft Outlook 2003.

  1. On the Project menu, click on ProjectName Properties
  2. Click on the Debug tab
  3. In the Start Action pane, click the Start external program radio button
  4. Click the file browser button and navigate to %ProgramFiles%Microsoft OfficeOffice12
  5. Choose Outlook.exe and click Open
  6. Press F5 to debug your add-in

For more details. Go through this article in Misha Shneerson’s Blog.
There are some tips and tricks to be followed and the things that one needs to take care while developing add-ins targeting multiple versions of Office in Andrew’s Blog. Have a look at this too. You might find it interesting and useful.
http://blogs.msdn.com/andreww/archive/2007/06/15/can-you-build-one-add-in-for-multiple-versions-of-office.aspx
Here is an MSDN article on Running Solutions in Different Versions of Microsoft Office
http://msdn.microsoft.com/en-us/library/bb772080.aspx
Hope this helps!