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.
Hello mr.pranav im new to this technology,so i am requested to you,can you able to forward some samples based on VSTO technology..my mail id is shankar.parsanamoni@gmail.com