CommandBarButton click event does not fire

C

Chris

I have added a button to the standard commandbar.
I added a click event:
cbbChefsChats.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(cbbChefsChats_Click);

When I click my button, nothing happens.

If I call the click method directly from code the messagebox shows

public void cbbChefsChats_Click(Office.CommandBarButton ctrl, ref bool
cancelDefault)
{
MessageBox.Show("clicked chefschats button");
}


Anyone know why this does not work?

TIA,

Chris
 
W

Wei Lu [MSFT]

Hello Chris,

I have tried on my side, it workes fine.

You could try the following code:
================
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;

namespace WordAddin2
{
public partial class ThisApplication
{
private Office.CommandBar AddInMenuBar;
private Office.CommandBarButton OpenTaskPaneButton;


private void ThisApplication_Startup(object sender,
System.EventArgs e)
{
try
{

AddInMenuBar =
this.ActiveWindow.Application.CommandBars.Add(
"Amazon", missing, missing, true);
OpenTaskPaneButton = (Office.CommandBarButton)
(AddInMenuBar.Controls.Add(
Office.MsoControlType.msoControlButton,
missing, missing, missing, true));
OpenTaskPaneButton.Caption = "Search Amazon";
OpenTaskPaneButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
OpenTaskPaneButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(
OpenTaskPaneButton_Click);
AddInMenuBar.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.Source,
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void OpenTaskPaneButton_Click(Office.CommandBarButton Ctrl,
ref bool CancelDefault)
{
MessageBox.Show("The button has been clicked");
}



private void ThisApplication_Shutdown(object sender,
System.EventArgs e)
{
OpenTaskPaneButton.Delete(false);
OpenTaskPaneButton = null;
AddInMenuBar = null;

}

#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(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);
}

#endregion
}
}
================

You could find the sample code in this article.

http://msdn2.microsoft.com/en-us/library/aa830702.aspx

Find the Migrating the VBA Global Template to an Add-In section.

Please let me know if this will work on your side.

Sincerely,

Wei Lu

Microsoft Online Community Support

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Chris

My button would work sometimes and not work others. Usually not work.
Check this out though, I just came across this KB article.
It seems that with Word (which I didn't mention is what I'm using) you must
set the "Tag" property of the commandbar.
http://support.microsoft.com/kb/826931

To my surprise, it now works.

Thank you for your help with this.

Chris
 
R

Raj

Hi,
I am also having the similar problem.. i am developing the COM
Office-PLugins using office 2003,C# and VS.NET 2005. I have a menu bar,
which contains 4 button. while i m opening multiple document my button
click event is
not fired for every document.my button click event is fired only for
the 1st document and not for others.

I have used Tag Property also.Assigned the unique Tag name for each of
the buttons.but still no Luck....
Can any body give some idea???
 
R

Raj

Hi,
I am also having the similar problem.. i am developing the COM
Office-PLugins using office 2003,C# and VS.NET 2005. I have a menu bar,
which contains 4 button. while i m opening multiple document my button
click event is
not fired for every document.my button click event is fired only for
the 1st document and not for others.

I have used Tag Property also.Assigned the unique Tag name for each of
the buttons.but still no Luck....
Can any body give some idea???
 
T

Tommie Johansson

Hi!
I have some problems with Word add-ins as well. My problem is that the
events won't get fired if a new document is created. I took your code and it
works fine until I click "New Document" on the toolbar. Then the event won't
get fired in the new document. However the messagebox shows up in the
original document.

regards

/Tommie Johansson
 

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