Show and Hide Context Menu Items in Outlook 2010 (Norm Estabrook)

Rate This
  • Comments 2

If you have written code that shows and hides context menu items in Outlook 2007, that code might not behave well in Outlook 2010. That is exactly what happened to this individual who recently posted to the VSTO forum.

The command bar object model (which most folks use to muck around with menus) has been deprecated in Outlook 2010. You can see a list of deprecated objects here - Outlook 2010 Object Model Changes Since Earlier Versions. Note that you can still use command bars, but be warned that “mileage may vary” and “objects in the mirror are closer than they appear”. etc. etc. You get the point.

Nice, so if not the command bar object model, then what? Well, for Outlook 2010 anyway, the answer is Ribbon XML.

In this post, I will show you how to show and hide items in a context menu by using Ribbon XML. The context menu appears when a user right-clicks an Outlook folder.

First, add a Ribbon XML item to your Outlook 2010 project.

image

Then add the following code to your ThisAddIn class. This example assumes that your Ribbon is called Ribbon1 and that you are using C#:

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()

{

    return new Ribbon1();

}

Next, replace the contents of the Ribbon1.xml file with the following:

<?xml version="1.0" encoding="UTF-8"?>

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">

    <contextMenus>

        <contextMenu idMso="ContextMenuFolder">

            <button idMso="FolderPropertiesContext" getVisible="IsVisible" />

        </contextMenu>

    </contextMenus>

</customUI>

Just to clarify, "FolderPropertiesContext" is the control identifier of the Properties command in the context menu.  You can obtain the complete list of control identifiers here - Office 2010 Help Files: Office Fluent User Interface Control Identifiers. So you can show/hide/enable/disable just about anything you want.

When Outlook is about to display the Properties command, Outlook first consults a cool method that you are going to write called "IsVisible".  You can name your method anything you want. You could name it "ShouldIShowThisMethodOrShouldINotShowThisMethod" or "NormsExamplesAreAlwaysVeryCoolandUseful“.  Totally up to you. It’s only important that you use the correct method signature.

So let’s write the ”IsVisible” method. Add the following method to the Ribbon1.xml file.

public bool IsVisible(Office.IRibbonControl control)
{
    string foldername = ((Microsoft.Office.Interop.Outlook.Folder)control.Context).Name;
    if (foldername == "Inbox")
    {
        return false;
    }
    return true;
}

This code grabs the context of the control that is passed into the method. In this case, the context is the currently selected folder.

If the user right-clicks the Inbox folder, then we tell Outlook to not show the command by returning false. Otherwise, we tell Outlook to go ahead and make the command visible by returning true.

But wait. There’s more!

After the first couple of times that you right-click a folder, Outlook takes a coffee brake and stops calling your method.  Actually, Outlook just caches the state of each control. If you want Outlook to call this method again, you have to “clear” that cache by “invalidating” the control.  Here is what I did to make that happen:

First, provide access to the RibbonUI object in your ThisAddIn class. The RibbonUI object enables you to “invalidate” the Properties command of the folder context menu. Add the following code to the ThisAddIn class of your project:

Office.IRibbonUI ribbonUI;
       
public Office.IRibbonUI RibbonUI
{
    get { return ribbonUI; }
    set { ribbonUI = value; }
}

Next, set the RibbonUI property of your ThisAddIn class within the Ribbon_Load event of the Ribbon code file as follows:

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
    this.ribbon = ribbonUI;
    Globals.ThisAddIn.RibbonUI = ribbonUI;
}

Finally, in the ThisAddIn class, handle the FolderContextMenuDisplay event of the Application object.

In the event handler of this event, “invalidate” the “FolderPropertiesContext” control.

Outlook.Application app;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    app = this.Application;
    app.FolderContextMenuDisplay += new Outlook.ApplicationEvents_11_FolderContextMenuDisplayEventHandler(app_FolderContextMenuDisplay);
 
}

void app_FolderContextMenuDisplay(Office.CommandBar CommandBar, Outlook.MAPIFolder Folder)
{
    RibbonUI.InvalidateControlMso("FolderPropertiesContext");
}    

You can find other useful info about how to extend the Outlook user interface here Extending the User Interface in Outlook 2010.

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
  • This code is fake!! :D THERE IS A NULLREFERENCEEXCEPTION ON LAST FUNCTION DESCRIBED void app.....

    Why?

  • The FolderContextMenuDisplay event is deprecated and not raised in Outlook 2013.  This makes it impossible to call Invalidate, which means that it is not possible to implement a folder context menu item that is only conditionally visible/enabled (i.e. make use of the getVisible or getEnabled callbacks).

Page 1 of 1 (2 items)

Show and Hide Context Menu Items in Outlook 2010 (Norm Estabrook)