Adding dynamic menus and events in Outlook

Hi,

I have a VSTO outlookin addin, where I am dynamically adding a number
of menu items (based on a dataset which is loaded from an XML file).

I have got the menu's adding correctly, but I am having trouble with
getting an event to fire for each dynamically created menu item. I
have the following code, which is called from ThisApplication.Startup:

public void addTopMenu()
{
object missing = System.Reflection.Missing.Value;
_menuBar = currentExplorer.CommandBars.ActiveMenuBar;
_topMenu =
_menuBar.Controls.Add(Office.MsoControlType.msoControlPopup, missing,
missing, missing, true) as Office.CommandBarPopup;
_topMenu.Caption = MENU_NAME;
_topMenu.Visible = true;
_LaunchMenu =
_topMenu.Controls.Add(Office.MsoControlType.msoControlPopup, missing,
missing, missing, true) as Office.CommandBarPopup;
_LaunchMenu.Caption = ADD_NAME;
_LaunchMenu.Visible = true;
_LaunchMenu.Enabled = true;

dsMenu = GetDataSet();

foreach (DataRow drTemp in dsMenu.Tables[0].Select())
{
_EventType =
_LaunchMenu.Controls.Add(Office.MsoControlType.msoControlButton,
missing, missing, missing, true) as Office.CommandBarButton;
_EventType.Tag = drTemp[0].ToString();
_EventType.Caption = drTemp[1].ToString();
_EventType.Visible = true;
_EventType.Enabled = true;
_EventType.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(_AddEvent_C-lick);
}

}

When I run the code, the event the first time I click on one of the
menu items, but not subsequently. I am a bit stuck now, so any help
would be much appreciated.

Thanks

Ian




Answer this question

Adding dynamic menus and events in Outlook

  • nomad#1

    Hi Ian,

    You're correct. If you use the same variable, then you're essentially assigning a new button to the variable for each iteration, and the variable can only refer to last assignment made.

    Kathleen McGrath
    http://blogs.msdn.com/vsto2/



  • Mr. Bob Dobalina

    You should declare your commandbar variables at the class level, rather than within the method (in your example, the declaration should be outside of addTopMenu). If you declare variables inside of the method, they will go out of scope as soon as the method is run, and the garbage collection can reallocate the memory.

    For more information on creating menus in Outlook, see http://msdn2.microsoft.com/en-us/library/ms268864(VS.80).aspx

    Kathleen McGrath
    http://blogs.msdn.com/vsto2/



  • SirSmokey

    As Kathleen has noted, you need to have the proper scope for your variable declarations.

    You should look at the VSTO Outlook code samples I list here:

    http://blogs.msdn.com/johnrdurant/archive/2005/12/07/vsto_outlook_resourcelist.aspx

    Our core samples have great examples on how to work with menus.

    John.



  • Prasant

    I still seem to be having a problem getting the events to fire. I have the CommandBarButton declared at class level, but I think the problem is that I am using the same variable multiple times within a foreach loop. The event just doesn't fire. If I don't use my foreach loop, and assign seperate variables for each button, then the events fire OK.

  • Adding dynamic menus and events in Outlook