This documentation is archived and is not being maintained.

Application.ItemContextMenuDisplay Event

Office 2007
Occurs before a context menu is displayed for a collection of Outlook items.

Version Information
 Version Added:  Outlook 2007


expression.ItemContextMenuDisplay(CommandBar, Selection)

expression   An expression that returns an Application object.


NameRequired/OptionalData TypeDescription
CommandBarRequiredCommandBarThe context menu to be displayed.
SelectionRequiredSelectionThe collection of Outlook items for which the context menu is to be displayed.


This event occurs before a context menu for either a single highlighted Outlook item or for one or more selected Outlook items is to be displayed, allowing the CommandBar object representing the context menu to be customized by an add-in.


The following Visual Basic for Applications (VBA) sample adds a menu item, as a CommandBarButton object, to the item context menu if a single MailItem object is selected in the active explorer. Selecting that menu item creates and displays a reply message addressed only to the recipients included in the To property of the selected MailItem object.

The sample code must be placed in a class module such as ThisOutlookSession.

Visual Basic for Applications
Private Sub Application_ItemContextMenuDisplay( _
    ByVal CommandBar As Office.CommandBar, _
    ByVal Selection As Selection)

    Dim objButton As CommandBarButton
    Dim intButtonIndex As Integer
    Dim intCounter As Integer
    On Error GoTo ErrRoutine
    ' Ensure we have only one item selected.
    If Selection.Count = 1 Then
        ' Ensure we have a MailItem selected.
        If Selection.Item(1).Class = olMail Then
            ' Find the location of the Reply To All button.
            For intCounter = 1 To CommandBar.Controls.Count
                If CommandBar.Controls(intCounter).ID = 355 Then
                    intButtonIndex = intCounter
                    Exit For
                End If
            ' If we have a Reply To All button in the
            ' context menu, add a new button to support
            ' the ReplyToNoncopied routine.
            If intButtonIndex <> 0 Then
                ' Create a new menu item and place it
                ' just after the Reply To All button
                Set objButton = CommandBar.Controls.Add( _
                    msoControlButton, , , intButtonIndex)
                ' Configure the menu item.
                With objButton
                    .Style = msoButtonIconAndCaption
                    .Caption = "Repl&y to Non-copied"
                    .Parameter = Selection.Item(1).EntryID
                    .FaceId = 355
                    ' If you place this sample in a class module
                    ' other than ThisOutlookSession, update this
                    ' line of code to ensure that the OnAction
                    ' property contains the correct project,
                    ' class, and routine name.
                    .OnAction = "Project1.ThisOutlookSession.ReplyToNoncopied"
                End With
            End If
        End If
    End If
    On Error GoTo 0
    ' Place clean-up code here.
    Exit Sub
    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
    GoTo EndRoutine
End Sub

Private Sub ReplyToNoncopied()
    Dim objNamespace As NameSpace
    Dim objItem As MailItem
    Dim objReplyItem As MailItem
    Dim objRecipient As Recipient
    Dim strEntryID As String
    On Error GoTo ErrRoutine
    ' Retrieve the entry ID of the selected message from
    ' the Parameter property of the button used to call
    ' this routine.
    strEntryID = _
    If strEntryID <> "" Then
        Set objNamespace = Application.GetNamespace("MAPI")
        Set objItem = objNamespace.GetItemFromID(strEntryID)
        Set objReplyItem = objItem.Reply
        ' If we have an entry ID, retrieve the item and
        ' create a reply message.
        With objReplyItem
            For Each objRecipient In objItem.Recipients
                ' Iterate through each recipient, adding
                ' only those recipients specified in the
                ' To property other than the current user.
                If objRecipient.Type = olTo Then
                    If objRecipient.Name <> Application.Session.CurrentUser.Name Then
                        .Recipients.Add objRecipient.Name
                    End If
                End If
        End With
        ' Display the new reply message.
    End If

    On Error GoTo 0
    Set objRecipient = Nothing
    Set objReplyItem = Nothing
    Set objItem = Nothing
    Set objNamespace = Nothing
    Exit Sub

    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
    GoTo EndRoutine
End Sub