none
Excel 2010 ContextMenu customization with XML - Ribbon_Load and other actions not firing... RRS feed

  • Question

  • I have - what should be - a simple situation where some of the key events are not firing. First, background:

    • This is an Excel 2010 code behind document with VS Professional 4.0 - where I stated with an existing Excel file. Normally I start with a new file, so do not know if there is something coming from there.
    • In my ribbon customization, no new tabs are desired, just the context menu changes.

    So, what I did was: to add a ribbon XML

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <contextMenus>
        <contextMenu idMso="ContextMenuCell" >
            <button id="btnTest"
                    getEnabled="GetEnabled"
                    visible="true"
                    label="Test Button"
                    onAction="OnAction"/>
         </contextMenu>
      </contextMenus>
    </customUI>

    Have put the code below in ThisWorkbook.cs

            protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
            {
                return new MyRibbon();
            }

    This is firing - and I see MyRibbon object being instantiated. Now, what I do not see happening is:

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

    This method is not being invoked. I was not sure if this is even needed since the ribbon itself is not being modified, and it is only the context menu that requires changes. But, that is the beginning of the problem. The other methods:

            public bool GetEnabled(Office.IRibbonControl control) {...}

    and

            public void OnAction(Office.IRibbonControl control) {...}

    are not being invoked either - and I do not have the context menu changes I am trying to do. After having banging my head against it for some time, I am hoping that a more experienced eye can point me in the right direction.

    Many thanks!

     

    Thursday, November 24, 2011 3:26 PM

Answers

  • I did manage to resolve the issue. In case it helps someone, I found that -

            public string GetCustomUI(string ribbonID)
            {...        }

    was returning null due to a mismatch in location of the XML file. Sorry for the trouble!

    • Marked as answer by kavm Friday, November 25, 2011 4:05 AM
    Friday, November 25, 2011 4:05 AM

All replies

  • Hi Kavm,

    Thanks for your post.

    It seems that your Ribbon customization fails to find the XML markup. Would you like to post your code of GetCustomUI method, which you can find by expanding IRibbonExtensibility Members

    Also you can set a breakpoint for this method to detect if this method is called to load your XML markup. Generally speaking, the GetCustomUI method is generated automatically, and you don't need to edit it if you don't have some particular requirement about the situation the custom ribbon should show.

    The automatically generated method should look like:

          public string GetCustomUI(string ribbonID)
            {
                return GetResourceText("YourProjectName.MyRibbon.xml");
            }
    

    I look forward to hearing of you soon.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Friday, November 25, 2011 3:43 AM
    Moderator
  • I did manage to resolve the issue. In case it helps someone, I found that -

            public string GetCustomUI(string ribbonID)
            {...        }

    was returning null due to a mismatch in location of the XML file. Sorry for the trouble!

    • Marked as answer by kavm Friday, November 25, 2011 4:05 AM
    Friday, November 25, 2011 4:05 AM