by Mathias 16. November 2008 12:26

Someone commented to my post on add-in menus asking if it was possible to add sub-menus to menus - the answer is yes. The code is essentially the same, with one small difference. When you add a menu item to a menu, you will add a CommandBarControl (the menu item) to the controls of a CommandBarPopup, the menu container. If you want to add a "nested" menu to the menu, instead of adding a CommandBarControl, you will add a CommandBarPopup, which can then receive menu items (or more nested menus!).

In code, it would look something like this:

// Add the sub-menu to parentMenu, which is a CommandBarPopup

CommandBarPopup parentCommandBarControl = (CommandBarPopup)parentMenu.Controls.Add(
MsoControlType.msoControlPopup, Type.Missing,
Type.Missing, Type.Missing, true);

parentCommandBarControl.Caption = "Sub-Menu";
parentCommandBarControl.Visible = true;

// Add the menu item to the sub-menu

CommandBarControl commandBarControl = parentCommandBarControl.Controls.Add(
MsoControlType.msoControlButton, Type.Missing,
Type.Missing, Type.Missing, true);

commandBarControl.Caption = menuItemCaption;
commandBarControl.Visible = true;

Comments

12/27/2008 9:02:09 PM #

Manuel

Awsome posts. Specially the one on about the menu. Thanks!

Manuel Mexico | Reply

3/9/2009 11:45:50 PM #

Mathias

Thank you Manuel! VSTO can be tricky at times, so whenever I struggle, I try to document it and hopefully spare others my pains...

Mathias | Reply

10/24/2009 3:03:44 AM #

darren mcevoy


hi - could you please update the menumanager class to work with a sub menu. i am having a too many casting issues when trying to implement the above.

        m_MenuManager = new MenuManager(this);
        m_MenuManager.CreateAddInMenu("My Add-In");
        m_MenuManager.AddMenuItem("Do This");
        m_MenuManager.AddSUBMenuItem("Do This1");
        m_MenuManager.AddMenuItem("Do That");
        m_MenuManager.AddSUBMenuItem("Do This2");

i added a new field to the class and 2 new methods but couldnt get it to work properly.

public List<Office.CommandBarPopup> m_MenuItems;
public List<Office.CommandBarButton> m_SubMenuItems;

AddSubMenuItem()
CreateSubMenuItem()



darren mcevoy United Kingdom | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading