Outlook addin on ribbon - Creates a new tab everytime I run the application

Hi, I have created an addin for Outlook, that will create a modal form when clicked.  The problem I am having, is that, everytime I run the program, I get a new tab appearing on the Outlook form.  How do I set it, such that, it will only display the one tab that I created.  See attached code, used in ThisAddin.cs.
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;
using System.Windows.Forms;

namespace OutlookModalForm
{
    public partial class ThisAddIn
    {
        private Office.CommandBar menuBar;
        private Office.CommandBarPopup newMenuBar;
        private Office.CommandBarButton buttonOne;
        private string menuTag = "A unique tag";
        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 = "Show Modal";
                    newMenuBar.Tag = menuTag;
                    buttonOne = (Office.CommandBarButton)newMenuBar.Controls.
                    Add(Office.MsoControlType.msoControlButton, missing,
                        missing, 1, true);
                    buttonOne.Style = Office.MsoButtonStyle.
                        msoButtonIconAndCaption;
                    buttonOne.Caption = "Link to Scheme";
                    buttonOne.FaceId = 65;
                    buttonOne.Tag = "c123";
                    buttonOne.Click += new
                        Office._CommandBarButtonEvents_ClickEventHandler(
                        buttonOne_Click);
                    newMenuBar.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }


        private void RemoveMenubar() 
        { 
            try 
            {        Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                     this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                     FindControl(Office.MsoControlType.msoControlPopup, missing, missing, true, true);
                     if (foundMenu != null) 
                     {
                         foundMenu.Delete(true);        
                     }    
            }
                     catch (Exception ex) 
                     {
                         MessageBox.Show(ex.Message);    
                     }
           }

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            RemoveMenubar();
            AddMenuBar();

        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion

        private void buttonOne_Click(Office.CommandBarButton ctrl, ref bool cancel)
        {
            ProcessMessages();
        }

        private Form1 form1 = null;
        private void ProcessMessages()
        {
            if (form1 == null)
            {
                form1 = new Form1(this.Application);
            }
            form1.ShowDialog();
        }


    }
}

Open in new window

NerishaBAsked:
Who is Participating?
 
xenacodeCommented:
(just to clarify...)

Right click on menu bar, click Customize, select Menu Bar, Click Reset to natively remove custom menu items.

Pete
0
 
xenacodeCommented:
Hi,

You are calling RemoveMenuBar before AddMenuBar to ensure that any previously created menu is deleted before adding a new one however you are not specifying anything for the ID or Tag arguments of the FindControl function. Consequently RemoveMenuBar will never actually find the menu or delete it. Try setting the Tag argument to menuTag.

Pete
Xenacode Ltd
0
 
NerishaBAuthor Commented:
Hi there,

Can you help me with where I need to put in the code??  I added the line in line 10.  Is this correct?
private void RemoveMenubar() 
        { 
            try 
            {        Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                     this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                     FindControl(Office.MsoControlType.msoControlPopup, missing, missing, true, true);
                        foundMenu.Tag = menuTag;
                     if (foundMenu != null) 
                     {
                         foundMenu.Delete(true);        
                     }    
            }
                     catch (Exception ex) 
                     {
                         MessageBox.Show(ex.Message);    
                     }
           }

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
xenacodeCommented:
No. I mean instead of this line in RemoveMenubar:

FindControl(Office.MsoControlType.msoControlPopup, missing, missing, true, true);

Use this line:

FindControl(Office.MsoControlType.msoControlPopup, missing, menuTag, true, true);

(You just need to supply the Tag parameter.)

Pete
Xenacode Ltd
0
 
NerishaBAuthor Commented:
I tried that line, but it makes no difference.  It seems that the ribbon buttons are drawn before the "RemoveMenuBar" is called. Any ideas?
0
 
xenacodeCommented:
Are you telling me that even after that change, you get an extra menu every time you start Outlook or are you saying that the extra menu items created by the previous version of your addin have not been removed?

If it's the first one then please check your code as I tried this myself and it worked. A clean install of this addin should be fine with the modified code.

To clean up the menus from a previous installation of the code that contained the error, you could either use the native menu cleanup features in Outlook or you could modify your code by adding a loop within RemoveMenubar around the block of code  that does the find & delete so that it loops through until foundMenu is null.

Pete
Xenacode Ltd
0
 
NerishaBAuthor Commented:
Thank you, I just reset the menuBar to remove the menu items.  Thanks alot!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.