Question

I have an Outlook 2007 add-in that I am trying to get to work in Outlook 2013. It is mostly working, but the additional context menu items that the add-in displays when right-clicking on emails and folders did not initially show up in Outlook 2013. Based on this answer I created a ribbon xml. The additional context menu items defined in the ribbon xml now show up in Outlook 2013.

However, I need to customize the context menu items based on the selection; for example, if the email is from a known email address, show a context menu item to move the email to a folder associated with that email address. I do this by handling ItemContextMenuDisplay in Outlook 2007, but that event no longer fires in Outlook 2013.

How do I modify, add & remove context menu items at run time with Outlook 2013?

This must be possible since this Add-in Express post mentions an ADXRibbonButton.PropertyChanging event. If they have the event in add-in express, it must also be possible to observe a similar event without Add-in Express?

Was it helpful?

Solution

You have to use Callback methods (specified in the XML file) to control whether or not an item is visible. In fact there are lots of Callbacks available you can specify to custom your context menu in the XML file (and they all start with get). e.g. getEnabled, getImage, getDescription, getVisible, etc.

So for example to control whether or not a button in the Contact Item context menu is visible, you would set the getVisible XML attribute to call a method in the class object you returned in the CreateRibbonExtensibilityObject() method (in the ThisAddIn.cs file).

Note You need to make sure the object you return in the CreateRibbonExtensibilityObject() method implements the MSOffice.IRibbonExtensibility interface.

e.g.

public partial class ThisAddIn : IThisAddInView
{
    protected override MSOffice.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        return new MyRibbonCallbacks();
    }
}

In the example below I tell the context menu button to call the GetVisible method to workout if it's visible or not.

<contextMenu idMso="ContextMenuContactItem">
  <menuSeparator id="Separator1" />

  <button id="AddNoteButton"
          label="Add note"
          onAction="OnActionButton"
          getVisible="GetVisible"/>
</contextMenu>

And in the GetVisible method workout if the menu item should be visible or not

public class MyRibbonCallbacks : MSOffice.IRibbonExtensibility
{
    public bool GetVisible(Microsoft.Office.Core.IRibbonControl control)
    {
        switch (control.Id)
        {
            case "AddNoteButton":
                // Work out if the button should be visible or not. 
                // And return true for visible. And false for invisible

                // Visible if current time is 11 o clock or after (just an example)
               return DateTime.Now.Hour >= 11;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top