Organizing Your E-mail Using Custom Item Context Menus in Outlook 2007

Today was my first day back from vacation, a day that I almost always dedicate to trying to clean up my Inbox. I’ve been meaning to write some VBA to help expedite moving e-mails to common folders, and this was the perfect opportunity. Until Outlook 2007, there was no way to customize any of the right-click context menus that are available throughout the application. No there are six different context menus that can be customized for various objects:

  • Attachment
  • Folder
  • Item
  • Shortcut
  • Store
  • View

The one I’m interested in is the Item menu, which is exposed via the Application.ItemContextMenuDisplay event. I think this is a perfect spot to add menu items for specific folders which I commonly move e-mails into. So that this:

Becomes this:

(the “PERMANENTLY DELETE” item is put in a special location with a new group at the end just for my purposes)

Okay, so let’s start coding. First thing – create a clsCustomContextMenus class in the VBA editor. We’ll come back to this, but after that go to your ThisOutlookSession module and make sure this code is there:

Now we’ll populate the clsCustomContextMenus class with the code we need:

The constants you see declared at the top of the class with the wacky strings are the unique folder IDs for the folders we want to move e-mails to. You can easily get these IDs by using Outlook Spy ( or with a VBA macro that you run with the folder active in Outlook:

Sub DisplayFolderEntryID()

InputBox “EntryID for folder ‘” & Application.ActiveExplorer.CurrentFolder.Name & “‘ = “, “SHOW ENTRYID”, Application.ActiveExplorer.CurrentFolder.EntryID

End Sub

Note also that we need to declare CommandBarButton objects – one for each folder, and a Folder object of course for each one. The _Click event that is fired for each menu item will pass the appropriate Folder object to the MoveToFolder procedure, which will loop through each selected message and move it to the destination folder. For the ItemContextMenuDisplay event, I’m also checking to make sure that at least one of the selected items is a MailItem or PostItem object. If none of these item types are present, none of the messages will be moved at all. You can of course change this to suit your purposes.

As a bonus, I’m including some sample code from Microsoft Outlook 2007 Programming: Jumpstart for Power Users and Administrators, Sue Mosher’s latest book which I was the technical editor for. When you right-click a store’s root folder, this code will show the path to the store’s .ost or .pst file (it won’t display that for IMAP stores). This code is in the StoreContextMenuDisplay event and will generate this when a store is right-clicked:

Eric Legault

Full-stack product builder & consultant for Microsoft 365 & Graph. Microsoft MVP 2003 - 2019. Outlook add-in guru. Rocker. Video/audio engineer. Collects Thors.

You may also like...

%d bloggers like this: