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:
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.
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:
- 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.
- 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. - 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.
- If a value was retrieved, an object reference representing the selected Outlook item is retrieved by using the GetItemFromID method of the NameSpace object.
- 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", theGetMyCalendarFolder
function routine is called. If the value is set to "CompanyEvents, theGetCompanyEventsFolder
function routine is called. - Finally, the action routine checks if a valid Folder object reference for the target folder was returned by by either the
GetMyCalendarFolder
orGetCompanyEventsFolder
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:
- The function uses the GetNamespace function of the Application object to retrieve a valid NameSpace object reference.
- The function then uses the GetDefaultFolder method of the NameSpace object to retrieve the Folder object reference that represents the default calendar folder.
- 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:
- 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.
- 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:
- It creates a new Folder object named "Company Events", representing the new calendar folder, in the Folders collection of the Calendar default folder.
- 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.
- 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.
- 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.
- Finally, the function returns the Folder object reference.
|