The event to use to add a toolbar to an Inspector window is Inspectors.NewInspector. In the Outlook VBA code below, note how the common operations of filling the combo box and processing a task are broken out into separate procedures that can be called for the combo box on either type of window:
' place all code in the built-in ThisOutlookSession module in Outlook VBA
Option Explicit
Dim WithEvents objCBC_E As Office.CommandBarComboBox
Dim WithEvents objCBC_I As Office.CommandBarComboBox
Dim WithEvents colInsp As Outlook.Inspectors
Private Sub Application_Startup()
Dim objExpl As Outlook.Explorer
Dim objCB As Office.CommandBar
On Error Resume Next
Set colInsp = Application.Inspectors
Set objExpl = Application.ActiveExplorer
Set objCB = objExpl.CommandBars("Standard")
Set objCBC_E = objCB.Controls.Add(Type:=msoControlComboBox, _
Temporary:=True)
Call FillCombo(objCBC_E)
Set objCB = Nothing
Set objExpl = Nothing
End Sub
Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objCB As Office.CommandBar
On Error Resume Next
Set objCB = Inspector.CommandBars("Standard")
Set objCBC_I = objCB.FindControl(msoControlComboBox, , "MyCombo")
If objCBC_I Is Nothing Then
Set objCBC_I = objCB.Controls.Add(Type:=msoControlComboBox, _
Temporary:=True)
Call FillCombo(objCBC_I)
End If
Set objCB = Nothing
End Sub
Private Sub objCBC_E_Change(ByVal Ctrl As Office.CommandBarComboBox)
Dim objExpl As Outlook.Explorer
Dim objTask As Outlook.taskItem
Dim objProp As Outlook.UserProperty
On Error Resume Next
Set objExpl = Application.ActiveExplorer
For Each objTask In objExpl.Selection
Call UpdateTask(objTask, objCBC_E.text)
Next
objCBC_E.text = "Select option here"
Set objProp = Nothing
Set objExpl = Nothing
Set objTask = Nothing
End Sub
Private Sub objCBC_I_Change(ByVal Ctrl As Office.CommandBarComboBox)
Dim objTask As Outlook.taskItem
Dim objProp As Outlook.UserProperty
On Error Resume Next
Set objTask = Application.ActiveInspector.CurrentItem
Call UpdateTask(objTask, objCBC_I.text)
objCBC_I.text = "Select option here"
Set objProp = Nothing
Set objTask = Nothing
End Sub
Sub FillCombo(cbc As Office.CommandBarControl)
' adapted from Help file
With cbc
.AddItem "Get Stock Quote", 1
.AddItem "View Chart", 2
.AddItem "View Fundamentals", 3
.AddItem "View News", 4
.DescriptionText = "View Data For Stock"
.text = "Select option here"
.Tag = "MyCombo"
.Width = 200
.Style = msoComboNormal
End With
End Sub
Sub UpdateTask(task As Outlook.taskItem, choice As String)
Dim objProp As Outlook.UserProperty
Dim strPropName As String
' set the name of the custom property here
strPropName = "My Custom Property Name"
Set objProp = task.UserProperties(strPropName)
' if the property doesn't exist, create it
If objProp Is Nothing Then
Set objProp = _
task.UserProperties.Add(Name:=strPropName, _
Type:=olText)
End If
objProp.Value = choice
task.Save
Set objProp = Nothing
End Sub
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54