Search code examples
c#excelvstoshared-addin

Create a sub menu in existing menu in Excel Shared Add-in


I am developing a Excel shared Add-in which has the menu called Custom which is created using Excel Macros. Now i want to create a submenu under the Custom menu using Csharp Shared Add -in. Iam using the below code for doing this but no help

oStandardBar = oCommandBars["Custom"];
oCmdBarCtrl = oStandardBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true);
oCmdBarCtrl.Visible = false;
oCmdBarCtrl.Caption = "Sub Menu1";

But it does not create a submenu, where as if i give "Help" in place of Custom i get the menu created. any work around for this?


Solution

  • Here's a quick sample to help you on your way:

            var m_toolbar = this.Application.CommandBars.Add("WpfAddIn",
                Office.MsoBarPosition.msoBarTop, false, true);
    
            var mainMenu = (Office.CommandBarPopup)m_toolbar.Controls
                .Add(Office.MsoControlType.msoControlPopup, 
                missing, missing, missing, true);
            mainMenu.Caption = "Main menu";
    
            var subMenu1 = (Office.CommandBarButton)mainMenu.Controls
                .Add(Office.MsoControlType.msoControlButton, 
                missing, missing, missing, true);
            subMenu1.Caption = "Sub menu 1";
            subMenu1.FaceId = 1958;
    
            var subMenu2 = (Office.CommandBarPopup)mainMenu.Controls
                .Add(Office.MsoControlType.msoControlPopup,
                missing, missing, missing, true);
            subMenu2.BeginGroup = true;
            subMenu2.Caption = "Sub menu 2";
    
            var subMenu2Button = (Office.CommandBarButton)subMenu2.Controls
                .Add(Office.MsoControlType.msoControlButton,
                missing, missing, missing, true);
            subMenu2Button.Caption = "Sub menu 1";
            subMenu2Button.FaceId = 1958;
    
            m_toolbar.Visible = true;