How to: Customize a Context Menu to Support Moving Items between Calendars

Outlook Developer Reference

Several different context menus can be customized, allowing add-ins to change, disable, or hide existing menu items as well as add or remove new menu items. You can then take advantage of the Outlook object model to extend Outlook and provide additional functionality.

This sample provides the following four routines to customize the selection context menu for an appointment item, allowing the user to move the selected appointment item between the default calendar folder and a calendar folder named "Company Events":

  • An event handler routine that, when the ItemContextMenuDisplay event for the Application object occurs, adds a new menu item to the bottom of the selection context menu.
  • An action routine, associated with the menu item, that retrieves a Folder object reference to the selected target folder and then moves the selected appointment item to that target folder.
  • A function, named GetMyCalendarFolder, that returns a Folder reference to the default calendar folder.
  • A function, named GetCompanyEventsFolder, that returns a Folder reference to the "Company Events" calendar folder.

The event handler routine performs the following actions:

  1. The sample first creates a CommandBarButton object in the command bar returned by the CommandBar parameter of the ItemContextMenuDisplay event, but only if one and only one AppointmentItem object is selected in the active explorer.

  2. It then configures the CommandBarButton:

    • The OnAction property is set to "Project1.ThisOutlookSession.MoveBetweenCalendars", the full name of the action that the menu item will take when selected. The value of this property corresponds to the full name of the action routine in this sample.
    • The Parameter property is set to the value of the EntryID for the selected Outlook item, which is retrieved from the Selection parameter of the ItemContextMenuDisplay event.
    • The Caption property is set depending on the parent folder of the selected appointment item. If the Name property of the parent Folder object is set to "Company Events", then the Caption property is set to "&Move to "My Calendar" folder"; otherwise, the Caption property is set to "&Move to ""Company Events" folder".
    • The Tag property is set depending on the parent folder of the selected appointment item. If the Name property of the parent Folder object is set to "Company Events", then the Tag property is set to "MyCalendar"; otherwise, the Tag property is set to "CompanyEvents".

After the event handler routine finishes execution, the item context menu is displayed. If the Display metadata menu item appended to the end of the item context menu is selected, the action routine that was specified in the OnAction property of the CommandBarButton representing that menu item is called. The action routine then performs the following actions:

  1. The action routine first retrieves the entry ID of the selected item from the Parameter property of the object returned by the Application.ActiveExplorer.CommandBars.ActionControl property. This value is used to retrieve an AppointmentItem object reference for the selected Outlook item.
  2. It then retrieves the Tag property value of the object returned by the Application.ActiveExplorer.CommandBars.ActionControl property into the variable, strTargetFolder. This value is used to determine the folder to which the selected item should be moved.
  3. It then checks if a value was retrieved from the Parameter property. If no value was retrieved, a message is displayed indicating that the selected Outlook item could not be retrieved.
  4. If a value was retrieved, an object reference representing the selected Outlook item is retrieved by using the GetItemFromID method of the NameSpace object.
  5. If a valid Outlook item was not retrieved, the action routine displays a dialog box describing the issue. If a valid Outlook item was retrieved, the sample then checks the value of the strTargetFolder variable to determine which of the two function routines provided in the sample should be called to retrieve the target folder. If the value is set to "MyComputer", the GetMyCalendarFolder function routine is called. If the value is set to "CompanyEvents, the GetCompanyEventsFolder function routine is called.
  6. Finally, the action routine checks if a valid Folder object reference for the target folder was returned by by either the GetMyCalendarFolder or GetCompanyEventsFolder functions. If a valid Folder reference was not returned, the action routine displays a dialog box describing the issue. If a valid Folder reference was returned, the action routine finally calls the Move method of the AppointmentItem object to move the selected appointment item to the target folder.

The GetMyCalendarFolder function returns a Folder object reference for the default calendar folder. The function performs the following actions:

  1. The function uses the GetNamespace function of the Application object to retrieve a valid NameSpace object reference.
  2. The function then uses the GetDefaultFolder method of the NameSpace object to retrieve the Folder object reference that represents the default calendar folder.
  3. The function finally returns the Folder object reference.

The GetCompanyEventsFolder function returns a Folder object reference for a calendar folder named "Company Events". The function performs the following actions:

  1. The sample obtains a Folder object reference for the Calendar default folder for the current user, by using the GetDefaultFolder method of the NameSpace object.
  2. The function then attempts to obtain a Folder object reference for a folder named "Company Events" by using the Item method from the Folders collection of the Calendar default folder. If a Folder object reference for the "Company Events" folder is not obtained, the function then creates a new Folder object representing the "Company Events" folder by performing the following actions:
    1. It creates a new Folder object named "Company Events", representing the new calendar folder, in the Folders collection of the Calendar default folder.
    2. The sample then obtains a reference to the NavigationPane object for the active explorer and uses the GetNavigationModule method of the NavigationModules collection to obtain a CalendarModule object reference.
    3. It then uses the GetDefaultNavigationGroup method of the NavigationGroups collection for the CalendarModule to obtain a NavigationGroup object reference to the My Calendars navigation group.
    4. The function then adds a new NavigationFolder object, based on the Folder object created earlier by the sample, to the navigation group by using the Add method of the NavigationFolders collection for that navigation group.
  3. Finally, the function returns the Folder object reference.
  Sub Application_ItemContextMenuDisplay( _
    ByVal CommandBar As Office.CommandBar, _
    ByVal Selection As Selection)
    
    Dim objButton As Office.CommandBarButton
    
    On Error GoTo ErrRoutine
    
    If Selection.Count = 1 Then
        If Selection.Item(1).Class = olAppointment Then
            ' Add a new button to the bottom of the CommandBar
            ' (which represents the selection context menu.)
            Set objButton = CommandBar.Controls.Add( _
                msoControlButton)
            
            With objButton
                .FaceId = 1000
                .OnAction = _
                    "Project1.ThisOutlookSession.MoveBetweenCalendars"
                .Parameter = Selection.Item(1).EntryID
                
                ' Determine the Caption and Tag property values
                ' of the menu item, depending on the folder
                ' that contains the selected appointment item.
                If Selection.Item(1).Parent.Name = "Company Events" Then
                    ' Move the item to the My Calendar default folder.
                    .Caption = "&Move to ""My Calendar"" folder"
                    .Tag = "MyCalendar"
                Else
                    ' Move the item to the Company Events folder.
                    .Caption = "&Move to ""Company Events"" folder"
                    .Tag = "CompanyEvents"
                End If
            End With
        End If
    End If
    
EndRoutine:
    Exit Sub
    
ErrRoutine:
    MsgBox Err.Number & " - " & Err.Description, _
        vbOKOnly Or vbCritical, _
        "Application_ItemContextMenuDisplay"
    GoTo EndRoutine
End Sub

Private Sub MoveBetweenCalendars()

Dim objNamespace As NameSpace
Dim objFolder As Folder
Dim objItem As Object

Dim strTargetFolder As String
Dim strEntryID As String

On Error GoTo ErrRoutine

' Retrieve the Parameter and Tag property values from the
' control that called this routine.
strEntryID = _
    Application.ActiveExplorer.CommandBars.ActionControl.Parameter

strTargetFolder = _
    Application.ActiveExplorer.CommandBars.ActionControl.Tag

' If there's no entry ID, we can't easily retrieve the item.
If strEntryID = "" Then
    MsgBox "An entry ID could not be retrieved from " & _
        "the selected menu item."
Else
    ' Fetch an item reference using the specified entry ID.
    Set objNamespace = Application.GetNamespace("MAPI")
    Set objItem = objNamespace.GetItemFromID(strEntryID)
    
    If objItem Is Nothing Then
        MsgBox "A reference for the Outlook item " & _
            "could not be retrieved."
    Else
        If strTargetFolder = "MyCalendar" Then
            ' Get a Folder object reference to the
            ' "My Calendar" default calendar folder.
            Set objFolder = GetMyCalendarFolder
        ElseIf strTargetFolder = "CompanyEvents" Then
            ' Get a Folder object reference to the
            ' "Company Events" calendar folder.
            Set objFolder = GetCompanyEventsFolder
        End If
            
        If objFolder Is Nothing Then
            ' Folder couldn't be retrieved.
            MsgBox "A reference for the target folder " & _
                "could not be retrieved."
        Else
            ' Add the selected item to the folder.
            objItem.Move objFolder
        End If
    End If
End If

EndRoutine: Set objItem = Nothing Set objFolder = Nothing Set objNamespace = Nothing

Exit Sub

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

Private Function GetMyCalendarFolder() As Folder Dim objNamespace As NameSpace Dim objCalendar As Folder

On Error GoTo ErrRoutine
    
' Retrieve the default calendar folder.
Set objNamespace = Application.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar)

EndRoutine: On Error GoTo 0

Set objNamespace = Nothing

' Return the Folder object reference.
Set GetMyCalendarFolder = objCalendar

Exit Function

ErrRoutine: MsgBox Err.Number & " - " & Err.Description, _ vbOKOnly Or vbCritical, _ "GetMyCalendarFolder" End Function

Private Function GetCompanyEventsFolder() As Folder Dim objNamespace As NameSpace Dim objCalendar As Folder Dim objFolder As Folder

Dim objPane As NavigationPane
Dim objModule As CalendarModule
Dim objGroup As NavigationGroup
Dim objNavFolder As NavigationFolder

On Error GoTo ErrRoutine

' First, retrieve the default calendar folder.
Set objNamespace = Application.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar)

' Attempt to retrieve a calendar folder named "Company Events".
On Error Resume Next
Set objFolder = objCalendar.Folders.Item("Company Events")
On Error GoTo ErrRoutine

' If the "Company Events" calendar folder could not be
' retrieved, attempt to create it and assign to it a
' NavigationFolder object.
If objFolder Is Nothing Then
    Set objFolder = objCalendar.Folders.Add("Company Events", olFolderCalendar)
        
    ' Get the NavigationPane object for the
    ' currently displayed Explorer object.
    Set objPane = Application.ActiveExplorer.NavigationPane
    
    ' Get the calendar module from the Navigation Pane.
    Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
    
    ' Get the "My Calendars" navigation group from the
    ' calendar module.
    With objModule.NavigationGroups
        Set objGroup = .GetDefaultNavigationGroup(olMyFoldersGroup)
    End With
    
    ' Create a navigation folder for the "Company Events"
    ' folder in the "My Calendars" navigation group.
    Set objNavFolder = objGroup.NavigationFolders.Add(objFolder)
End If

EndRoutine: On Error GoTo 0

Set objNavFolder = Nothing
Set objGroup = Nothing
Set objModule = Nothing
Set objPane = Nothing
Set objNamespace = Nothing

' Return the Folder object reference.
Set GetCompanyEventsFolder = objFolder

Exit Function

ErrRoutine: MsgBox Err.Number & " - " & Err.Description, _ vbOKOnly Or vbCritical, _ "GetCompanyEventsFolder" End Function