NerishaB
asked on
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();
}
}
}
ASKER
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?
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);
}
}
No. I mean instead of this line in RemoveMenubar:
FindControl(Office.MsoCont rolType.ms oControlPo pup, missing, missing, true, true);
Use this line:
FindControl(Office.MsoCont rolType.ms oControlPo pup, missing, menuTag, true, true);
(You just need to supply the Tag parameter.)
Pete
Xenacode Ltd
FindControl(Office.MsoCont
Use this line:
FindControl(Office.MsoCont
(You just need to supply the Tag parameter.)
Pete
Xenacode Ltd
ASKER
I tried that line, but it makes no difference. It seems that the ribbon buttons are drawn before the "RemoveMenuBar" is called. Any ideas?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, I just reset the menuBar to remove the menu items. Thanks alot!
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