CommandBarButton events don't fire after new document.

T

Tommie Johansson

Hi!
I have a problem with word addins. When I create a custom CommandBar with
CommandBarButtons (yes, I define them at class level so the garbage collector
won't kill em) they work fine, until I click "New document" toolbar button.
The events wont fire when the "second" word app starts.

I've tried the following code ( posted by Wei Lu [MSFT] in response to the
"CommandBarButton click event does not fire" 1/17/2007).

Can someone please at least try the code and see if you get the same error??

thanks

/Tommie


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
}
}
 
C

Cindy M.

Hi =?Utf-8?B?VG9tbWllIEpvaGFuc3Nvbg==?=,
I have a problem with word addins. When I create a custom CommandBar with
CommandBarButtons (yes, I define them at class level so the garbage collector
won't kill em) they work fine, until I click "New document" toolbar button.
The events wont fire when the "second" word app starts.
Did you also declare a TAG property for the button? Word needs that to "sort"
which window is calling. I don't see an assignment to the Tag property in the
sample code...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
T

Tommie Johansson

Thanks Cindy.
It works if I put something in the tag property!

/Tommie
 

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