Here is a Way to Get the ID of a Built-in Outlook Command Bar Menu (Norm Estabrook)

Recently, a forum poster asked us how he could add a submenu item to a built-in menu item in Outlook.  Note that these are not controls that appear on the Ribbon of an Outlook item, but rather the menus that drop down from the top of the Outlook Explorer such as the View menu and the Tools menu.

So one way to do this (In fact the only way that I know of) is to use the example shown in the following MSDN topic – How to: Add Custom Menus and Menu Items to Outlook.

You can’t just use the example as is. You will need to add a line of code to get a handle to a built-in menu. The following example gets a handle to the Junk E-Mail menu that appears off of the Actions menu of the Outlook Explorer window.

Office.CommandBarPopup junkEmailMenu = (Office.CommandBarPopup)
    (Office.MsoControlType.msoControlPopup, 31353, missing, missing);

Note the ID number shown in the second parameter to the FindControl method. How on earth did I know that the Junk E-Mail menu is the 31353 menu?  The answer is … Not very easily. In fact to obtain that ID, I wrote a small Outlook add-in that iterates through all menus in Outlook and prints out their corresponding codes.  I figured that I would share this with you in case you ever have a similar scenario.

Two notes about the sample below: First – this is a C# example, so my apologies to VB’ers. Second – this example is meant to be used in a Visual Studio 2008 Outlook (2007 or 2003) add-in project.  These projects automatically include the appropriate using statements that enable you to use the prefix “Office” in place of “Microsoft.Office.Core” when referring to some objects etc.

Ok. Here is the code:

public partial class ThisAddIn
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
        Outlook.Application app = this.Application;
        //Create a new txt file to record controls' list

        System.IO.StreamWriter sw = System.IO.File.CreateText
            (@"C:\Outlook Menus.txt");

        //loop through Outlook ActiveExplorer CommandBars to get all CommandBars

        foreach (Office.CommandBarControl cb in app.ActiveExplorer().CommandBars.ActiveMenuBar.Controls)
            PrintMenuItems(cb, sw);



    // Recursive method for printing menus and nested menus.
    // Either the menu is a commandbarpopup (contains submenus)
    // Or it is a commandbarbutton (contains no submenus)

    private void PrintMenuItems(object menuItem, System.IO.StreamWriter sw)
        if (menuItem as Office.CommandBarButton == null)
            // This is a menu bar popup control.
            sw.WriteLine((menuItem as Office.CommandBarPopup).Caption +
                "\t" + (menuItem as Office.CommandBarPopup).Id.ToString());

            if ((menuItem as Office.CommandBarPopup).accChildCount > 0)
                for (int j = 1; j <= (menuItem as Office.CommandBarPopup).accChildCount; j++)
                    PrintMenuItems((menuItem as Office.CommandBarPopup).get_accChild(j), sw);
            // Must be a command
            sw.WriteLine("\t" + (menuItem as Office.CommandBarControl).Caption +
                "\t" + (menuItem as Office.CommandBarControl).Id.ToString());



Norm E.