In my VSTO addin, I pop a custom menu if someone right clicks a cell containing a valid bond CUSIP (finance industry jargon for a bond id). The method below accomplishes this using CommandBars. The question I have is how to accomplish the same using XML ribbon definitions. Here is the current code using command bars (works fine):
private static Office.CommandBar cbBondPopup = null;
private void Application_SheetBeforeRightClick(object Sh, Excel.Range Target, ref bool Cancel)
{
Cancel = false; // overridden to true by btn1, btn2, btn3 handlers below (so that Excel Cell Menu is cancelled)
if (IsBondCusip(this.Application.ActiveCell.Value2))
{
if (cbBondPopup == null && IsBondCusip(this.Application.ActiveCell.Value2))
{
// create command bar if nec
cbBondPopup = this.Application.CommandBars.Add(Name: "BondMenu", Position: Office.MsoBarPosition.msoBarPopup, Temporary: true);
// define 3 custom buttons
var btn1 = (Office.CommandBarButton)cbBondPopup.Controls.Add(Type: Office.MsoControlType.msoControlButton, Temporary: true);
var btn2 = (Office.CommandBarButton)cbBondPopup.Controls.Add(Type: Office.MsoControlType.msoControlButton, Temporary: true);
var btn3 = (Office.CommandBarButton)cbBondPopup.Controls.Add(Type: Office.MsoControlType.msoControlButton, Temporary: true);
btn1.Caption = "Bond Summary"; btn2.Caption = "Bond Details"; btn3.Caption = "Find Similar Bonds";
// connect buttons to handlers ==> CmdBarEventHndlr(CommandBarButton Ctrl, ref bool Cancel)
btn1.Click += Click_BondSummary; btn2.Click += Click_BondDetails; btn3.Click += Click_FindSimilarBonds;
}
cbBondPopup.ShowPopup(); // pop bond menu (handlers set Cancel to true so Excel Cell Menu is cancelled unless user hits ESC)
}
}
Next, here are the same buttons defined in XLM ribbon syntax. In this case the onAction methods have a different signature - e.g. Click_BondSummary(Office.IRibbonControl ctrl).
<button id="btnBondSummary" label="BondSummary" onAction="Click_BondSummary"/>
<button id="btnBondDetails" label="BondDetails" onAction="Click_BondDetails"/>
<button id="btnFindBonds" label="FindBonds" onAction="Click_FindSimilarBonds"/>
I was hoping I could just put these definitions into an XML context menu and pop that. By setting idMso="ContextMenuCell" I can cause the buttons to show up in the Excel Cell menu itself - but that's not what I want. I want them to pop up in their own standalone popup menu.
QUESTION: Is there a way to rewrite my right-click handler using XML ribbon button definitions instead of command bars? (Otherwise I need two definitions of everything - one for the ribbon and one for the command bar popup).