I'm using Visual Studio for office to create an Excel Addin. I've created a sample menu, with 2 methods. If I select a menu item, it displays my dialog box. After I close it my menu will not display again. It won't drop down when clicked on. Do you know why?
If you click choice1 or choice2, then you are done, can't do it twice. Code below:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
#region VSTO generated code
this.Application = (Excel.Application)Microso
ft.Office.
Tools.Exce
l.ExcelLoc
ale1033Pro
xy.Wrap(ty
peof(Excel
.Applicati
on), this.Application);
#endregion
CheckIfMenuBarExists();
AddMenuBar();
}
// Create the menu, if it does not exist.
private void AddMenuBar()
{
try
{
Office.CommandBarButton menuCommand;
Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)Applica
tion.Comma
ndBars.Act
iveMenuBar
;
int controlCount = menubar.Controls.Count;
string menuCaption = "&Stockwatch Quotes";
// Add the menu.
cmdBarControl = (Office.CommandBarPopup)me
nubar.Cont
rols.Add(
Office.MsoControlType.msoC
ontrolPopu
p, missing, missing, controlCount, true);
if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;
Office.CommandBarButton m1, m2;
// Add the menu command.
m1 = (Office.CommandBarButton)c
mdBarContr
ol.Control
s.Add(
Office.MsoControlType.msoC
ontrolButt
on, missing, missing, missing, true);
m1.Caption = "&Choice 1...";
m1.Tag = "choice1";
m1.FaceId = 65;
m1.Click += new Microsoft.Office.Core._Com
mandBarBut
tonEvents_
ClickEvent
Handler(
choice1_Click);
m2 = (Office.CommandBarButton)c
mdBarContr
ol.Control
s.Add(
Office.MsoControlType.msoC
ontrolButt
on, missing, missing, missing, true);
m2.Caption = "&Choice 2...";
m2.Tag = "choice2";
m2.FaceId = 65;
m2.Click += new Microsoft.Office.Core._Com
mandBarBut
tonEvents_
ClickEvent
Handler(
choice2_Click);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message)
;
}
}
private void choice1_Click(Microsoft.Of
fice.Core.
CommandBar
Button Ctrl, ref bool CancelDefault)
{
MessageBox.Show("Choice 1");
CancelDefault = false;
}
private void choice2_Click(Microsoft.Of
fice.Core.
CommandBar
Button Ctrl, ref bool CancelDefault)
{
MessageBox.Show("Choice 2");
CancelDefault = false;
}
Start Free Trial