Hi Experts
I have a problem with a Visual Studio 2008 Outlook Add-In that creates a main menu item on the Outlook Menu with 2 submenus. For some reason it only removes the submenus and not the Main menu item.
What happens in turn is that the main menu remains and it recreates the main menu with the 2 submenus. The old main menu is there but does not serve any purpose and makes Outlook look very funny especially after there is about 5 of the same main menu items but only one works.
Please see my code for creating and removing the menu. Is there something missing?
Thanks in advance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
private Office.CommandBar menuBar;
private Office.CommandBarPopup newMenuBar;
private Office.CommandBarButton btnRecording;
private Office.CommandBarButton btnRegistration;
private string menuTag = "Main Menu";
private string recmenuTag = "Menu Item 1";
private string regmenuTag = "Menu Item 2";
private void AddMenuBar()
{
try
{
menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
Office.MsoControlType.msoControlPopup, missing,
missing, missing, false);
if (newMenuBar != null)
{
newMenuBar.Caption = "Main Menu Bar Item";
newMenuBar.Tag = menuTag;
btnRecording = (Office.CommandBarButton)newMenuBar.Controls.
Add(Office.MsoControlType.msoControlButton, missing,
missing, 1, true);
btnRecording.Style = Office.MsoButtonStyle.
msoButtonIconAndCaption;
btnRecording.Caption = "Menu 1";
btnRecording.FaceId = 2986;
btnRecording.Tag = recmenuTag;
btnRecording.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
btnRecording_Click);
btnRegistration = (Office.CommandBarButton)newMenuBar.Controls.
Add(Office.MsoControlType.msoControlButton, missing,
missing, 1, true);
btnRegistration.Style = Office.MsoButtonStyle.
msoButtonIconAndCaption;
btnRegistration.Caption = "Menu 2";
btnRegistration.FaceId = 2936;
btnRegistration.Tag = regmenuTag;
btnRegistration.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
btnRegistration_Click);
newMenuBar.Visible = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void RemoveMenubar()
{ // If the menu already exists, remove it.
try {
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.FindControl(Office.MsoControlType.msoControlPopup,
missing, menuTag, true, true);
if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
AddMenuBar();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
RemoveMenubar();
}
Select all Open in new window
maybe you're looking for this:
Open in new window