Lost Menu Events in Excel / C# Addin - Please Help!

A

Armin Zoechel

Hi!

I am very desperate, this problem just drives me crazy!

I built an Excel-Addin in C# 2003 using the MSDN Howto
302901. It works fine the way it is described there. But
when I change the code so that I use a simple menu entry
in Excel's main menu instead of the button, I get the
annoying problem that the code of the menu event click
handler is executed about 40-50 times and then suddenly
stops responding to any clicks! The addin is still loaded
in memory and terminates correctly when I exit Excel,
although.

I am using Windows XP, VS .NET 2003 and Office XP & 2000
(the problem is the same, regardless of the Office
version). Latest service packs.

So what am I doing wrong? I would be most grateful to any
help! Many thanks in advance!

This is the code of the On StartupComplete method. The
rest of the code is exactly the same as in the MSDN
example.

private CommandBarPopup myMenu;

public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oMenuBar;

oCommandBars = (CommandBars)applicationObject.GetType
().InvokeMember("CommandBars", BindingFlags.GetProperty ,
null, applicationObject ,null);

oMenuBar = oCommandBars["Worksheet Menu Bar"];

// In case the button was not deleted, use the existing
one.
try
{
myMenu = (CommandBarPopup)oMenuBar.Controls["My&Menu"];
}
catch(Exception)
{
object oMissing = System.Reflection.Missing.Value ;

myMenu = (CommandBarPopup) oMenuBar.Controls.Add
(MsoControlType.msoControlPopup, oMissing, oMissing,
oMenuBar.Controls.Count, oMissing);
myMenu.Caption = "My&Menu";
CommandBar newMenu = myMenu.CommandBar;
CommandBarButton newMenuItem = (CommandBarButton)
newMenu.Controls.Add(MsoControlType.msoControlButton,
oMissing, oMissing, oMissing, oMissing);
newMenuItem.Caption = "&Test";
newMenuItem.Tag = "Test Tag";
newMenuItem.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHan
dler(this.MyButton_Click);
}

myMenu.Tag = "My Menu Tag";

System.Windows.Forms.MessageBox.Show("This Addin is
loaded!", "MyCOMAddin");
oMenuBar = null;
oCommandBars = null;
}
 
A

Armin Zoechel

Hi Sunny,

Thank you very much for your help!! That fixed it!
-----Original Message-----
Hi,
Make sure you keep a reference to that button all the time. I had the same
problem. Maybe the garbage collector decides to release the object at some
point, because there is no reference to the button. I mean, if you create a
button in a method, and you declare the button object in that method, after
that you set an event handler and leave the method, the reference to the
button goes out of scope. Because of setting the event, interop mechanism
keeps a reference to the event handler for some time only. Maybe it is a
good idea to define the button object public static somewhere.
At least that was my problem with Outlook 2000.

Hope that helps
Sunny

Hi!

I am very desperate, this problem just drives me crazy!

I built an Excel-Addin in C# 2003 using the MSDN Howto
302901. It works fine the way it is described there. But
when I change the code so that I use a simple menu entry
in Excel's main menu instead of the button, I get the
annoying problem that the code of the menu event click
handler is executed about 40-50 times and then suddenly
stops responding to any clicks! The addin is still loaded
in memory and terminates correctly when I exit Excel,
although.

I am using Windows XP, VS .NET 2003 and Office XP & 2000
(the problem is the same, regardless of the Office
version). Latest service packs.

So what am I doing wrong? I would be most grateful to any
help! Many thanks in advance!

This is the code of the On StartupComplete method. The
rest of the code is exactly the same as in the MSDN
example.

private CommandBarPopup myMenu;

public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oMenuBar;

oCommandBars = (CommandBars)applicationObject.GetType
().InvokeMember("CommandBars", BindingFlags.GetProperty ,
null, applicationObject ,null);

oMenuBar = oCommandBars["Worksheet Menu Bar"];

// In case the button was not deleted, use the existing
one.
try
{
myMenu = (CommandBarPopup)oMenuBar.Controls ["My&Menu"];
}
catch(Exception)
{
object oMissing = System.Reflection.Missing.Value ;

myMenu = (CommandBarPopup) oMenuBar.Controls.Add
(MsoControlType.msoControlPopup, oMissing, oMissing,
oMenuBar.Controls.Count, oMissing);
myMenu.Caption = "My&Menu";
CommandBar newMenu = myMenu.CommandBar;
CommandBarButton newMenuItem = (CommandBarButton)
newMenu.Controls.Add(MsoControlType.msoControlButton,
oMissing, oMissing, oMissing, oMissing);
newMenuItem.Caption = "&Test";
newMenuItem.Tag = "Test Tag";
newMenuItem.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHan
dler(this.MyButton_Click);
}

myMenu.Tag = "My Menu Tag";

System.Windows.Forms.MessageBox.Show("This Addin is
loaded!", "MyCOMAddin");
oMenuBar = null;
oCommandBars = null;
}


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top