Context Menu Assistance

Feb 2, 2015 at 5:03 AM
I’ve created a basic C# add-in that provides some functionality and am using a StringBuilder to create my Ribbon UI (code below), which all works perfectly for the Ribbon.

I can't seem to get it working for the Context Menu though, having tried a few different ways but no success.

Is it better to do this via the SheetBeforeRightClick event? And is there are any examples on how to do so. I can do it in VBA, but struggling a bit in C#.
public override string GetCustomUI(string RibbonID)
{
    StringBuilder sb = new StringBuilder();
    
    // Build the Ribbon:
    sb.AppendLine().AppendLine();
    sb.Append("<customUI xmlns='http://schemas.microsoft.com/office/2006/01/customui' >");                
    sb.AppendLine().Append("<ribbon>");                
    sb.AppendLine().Append("<tabs>");
    sb.AppendLine().Append("<tab id='Foo' label='" + _Name + "'>");
    sb.AppendLine().Append("<group id='GroupFoo' label='Foo Group'>");
    sb.AppendLine().Append("<button id='ButtonFoo' label='Foo Button' screentip='A Button for Foo' onAction='RunTagMacro' tag='fooBar' />");
    sb.AppendLine().Append("</group>");
    sb.AppendLine().Append("</tab>");
    sb.AppendLine().Append("</tabs>");
    sb.AppendLine().Append("</ribbon>");
    
    // Build the Context Menu -> Can't get this part to work ?
    sb.AppendLine().Append("<contextMenus>");
    sb.AppendLine().Append("<contextMenu idMso=\"ContextMenuCell\">");
    sb.AppendLine().Append("<menu id=\"SubMenuFoo\" label=\"Foo SubMenu\" insertAfterMso=\"Hyperlink\">");
    sb.AppendLine().Append("<button id=\"cmFoo\" label=\"Foo Context Menu\" imageMso=\"SlideMasterTextPlaceholderInsert\" onAction=\"RunTagMacro\" tag=\"Bar\"/>");
    sb.AppendLine().Append("</menu>");
    sb.AppendLine().Append("<menuSeparator id=\"MySeparator\" insertAfterMso=\"Hyperlink\" />");
    sb.AppendLine().Append("</contextMenu>");
    sb.AppendLine().Append("</contextMenus>");
    
    sb.AppendLine().Append("</customUI>");
    return sb.ToString();
}
Coordinator
Feb 2, 2015 at 8:04 AM
The ContextMenu support via the Ribbon xml is only available in Excel 2010 and later, and requires you to use the updated xml namespace: xmlns='http://schemas.microsoft.com/office/2009/07/customui'.

You can also use the old CommandBars interface to create context menus. You'll find various discussions on the Google group: https://groups.google.com/forum/#!searchin/exceldna/context$20menu

-Govert