Dmitriy Ovdienko

Sometimes Excel 2003 does not fires CommandBarButton's event.
I create CommandButton under CommandBarPopup of ActiveMenuBar in ThisAddIn_Startup
using following code:

CommandBarPopup menuItem = (CommandBarPopup)Application.CommandBars.ActiveMenuBar.Controls.Add(
MsoControlType.msoControlPopup, missing, missing, missing, true );

menuItem.Caption = "111";

CommandBarButton btnConnect_;
btnConnect_ = (CommandBarButton)menuItem.Controls.Add(
MsoControlType.msoControlButton, missing, missing, missing, missing );
btnConnect_.Click += new _CommandBarButtonEvents_ClickEventHandler( btnConnect_Click );
btnConnect_.Caption = "Connect";

The event handler is:

private void btnConnect_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
System.Windows.Forms.MessageBox.Show("1212");
}

So, when Excel starts it creates "111" menu and "Connect" button under it.
When I click "Connect" button nothing is happened. Sometimes it is faired
but next will might be non executable.

PS. Is raised unhandled exception will turn off event handler too



Re: Visual Studio Tools for Office CommandBarButton event sometimes is not faired

ClaudiaHelpOnVSTO

Try to set the TAG property. See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1252848&SiteID=1

Claudia




Re: Visual Studio Tools for Office CommandBarButton event sometimes is not faired

Dmitriy Ovdienko

I've updated tag property to "111_Connect". No result.

PS. What does it mean "buttons at the class level"





Re: Visual Studio Tools for Office CommandBarButton event sometimes is not faired

ClaudiaHelpOnVSTO

Dmitriy Ovdienko wrote:

I've updated tag property to "111_Connect". No result.

PS. What does it mean "buttons at the class level"



Here an example of a class level declaration:

namespace x
{
public partial class ThisDocument
{
// Toolbar buttons:
// toolbar variables must be declared at the class level
// to avoid garbage collection.
private Office.CommandBar toolbar;
private Office.CommandBarButton button;

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

The class level declaration + setting the TAG property worked fine for me (Word 2003).

Claudia





Re: Visual Studio Tools for Office CommandBarButton event sometimes is not faired

Dmitriy Ovdienko

It is so strange. Why does subscribing to COM event not make strong reference In WinForms, for instance, I can create button subscribe for Click event and forget about button reference.





Re: Visual Studio Tools for Office CommandBarButton event sometimes is not faired

Misha Shneerson - MSFT

Dmitriy, please see this article which tries to explain the problem also known as "Why Has My Menu Button Stopped Working in Office "