troubleshooting Question

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

Avatar of esps
esps asked on
.NET ProgrammingC#Outlook
2 Comments1 Solution947 ViewsLast Modified:
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();           
        }
ASKER CERTIFIED SOLUTION
esps

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros