Dynamic disable/enable custom context menu in Excel 2013

Feb 12, 2014 at 2:21 PM
Edited Feb 12, 2014 at 2:21 PM
Hello,
My addin is written in c#, using Excel-DNA, .NET Office
I add 2 context menu items in cell's context menu.
When you right click a cell, based on the formula of the cell, one context menu item will be disabled.
I put code for this in sheetSelectionChangeEvent
This works fine in Excel 2003, 2007 and 2010 but it does not work in Excel 2013.
This is a bug in Excel 2013 reported @ https://connect.microsoft.com/VisualStudio/feedback/details/813453/enabled-state-does-not-change-for-context-menu-items-of-excel-2013-add-in-created-with-c-net-and-visual-studio-2012
I tried the workaround but it does not work for me.
Thanks

Below is my code
private void ApplicationSheetSelectionChange(COMObject sh, Range target)
{
 DisableMenubarsButtonsWRibbon(XLApp.Selection as Range);
}

public void DisableMenubarsButtonsWRibbon(Range rng)
{
 var formula = rng.Formula as string;
 if(formula is function1)
 {
     SetEnableForOneContextMenuExcel2013(true, _contextMenuItem1);  
     SetEnableForOneContextMenuExcel2013(false, _contextMenuItem2);  
     //_contextMenuItem1.Enabled = true;
     //_contextMenuItem2.Enabled = false;
 }
 else if(formula is function2)
 {
     SetEnableForOneContextMenuExcel2013(false, _contextMenuItem1);  
     SetEnableForOneContextMenuExcel2013(true, _contextMenuItem2);  
     //_contextMenuItem1.Enabled = false;
     //_contextMenuItem2.Enabled = true;
 }
 else
 {
     SetEnableForOneContextMenuExcel2013(true, _contextMenuItem1);  
     SetEnableForOneContextMenuExcel2013(true, _contextMenuItem2);          
    //_contextMenuItem1.Enabled = true;
     //_contextMenuItem2.Enabled = true;
 }
}
    private void SetEnableForOneContextMenuExcel2013(bool enable, CommandBarButton commandBar)
    {
        var contextMenu = _contextMenuBars["Cell"];
        if (contextMenu == null) return;
        CommandBarButton resultingButton = null;
        foreach (CommandBarControl control in contextMenu.Controls)
        {
            if (control.Caption == commandBar.Caption)
            {
                resultingButton = control as CommandBarButton;
                if (resultingButton != null) resultingButton.Enabled = enable;
                break;
            }
        }
        contextMenu.Dispose();
    }