Avatar of esps
esps
 asked on

How do I Remove Menu Items created by Outlook Add-In?

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();           
        }

Open in new window

.NET ProgrammingC#Outlook

Avatar of undefined
Last Comment
StehtimSchilf

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
esps

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
StehtimSchilf

Hi esps

maybe you're looking for this:


IEnumerator e = menuBar.Controls.GetEnumerator();
while (e.MoveNext()) {
   newMenuBar = (Office.CommandBarPopup) e.Current;
   MessageBox.Show(newMenuBar.Caption +" " + newMenuBar.Tag);
}

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23