I'm trying to write application-level add-in for Word 2003. The plugin adds a button on a new commandbar - clicking the button saves active document and then performs some additional actions. When I launch Word 2003 and then click my commandbar button everything works fine. However if I launch Word 2003, open a new Word window by clicking toolbar button "New document" on a "Standard" toolbar and then click my commandbar button it turns out that no action is performed. It seems that my toolbar button on a new opened window has no "onclick" event handler assigned. Do you have any idea how to solve the problem ?
My add-in code is based on the code below:private Office.CommandBar commandBar;
private Office.CommandBarButton docSaveButton;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// prepare toolbar:
try
{
commandBar = Application.CommandBars["MY_TOOLBAR"];
}
catch (ArgumentException)
{
//...
}
if (commandBar == null)
{
commandBar = Application.CommandBars.Add("MY_TOOLBAR", 1, missing, true);
}
commandBar.Visible = true;
// addbutton:
docSaveButton = (Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing, missing, missing);
docSaveButton.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIcon;
docSaveButton.Caption = "My save";
docSaveButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(docSaveButtonClick);
}
private void docSaveButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
{
MessageBox.Show("Hello !", "Hello !", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Reg开发者_StackOverflowards JanK
I suspect your "add-in" is not loaded, but your toolbar is being persisted. Have you placed your "add-in" in one of Word's startup locations?
Frequently asked questions about the location of templates in Word 2003 or in Word 2007, Q3: Where Are My Word Add-in Files Saved?, http://support.microsoft.com/kb/826867.
Loading a Word Add-in, second bullet, http://msdn.microsoft.com/en-us/library/aa165426(office.10).aspx
•Automatically when Word starts, by saving the template file in the Word Startup folder on your computer. The default path to this folder is C:\Windows\Application Data\Microsoft\Word\Startup; if you're using user profiles, the default path is C:\Windows\Profiles\UserName\Application Data\Microsoft\Word\Startup. You can change this path in the Options dialog box
Haven't done this in Word, but I believe in Outlook I got it to work by listening for NewWindow Events (called Explorers and Inspectors in Outlook), and re-adding the button when a new window is created (and using "true" as the last parameter in commandBar.Controls.Add to make the button "temporary" so you won't end up with two of 'em).
P.S. I agree it should work like you expect, and am not sure why this is needed (or how it should ever work if "temp" is "false").
I ran into the same problem and resolved it by setting the Tag property on the buttons. This is by design it seems.
http://support.microsoft.com/kb/826931
精彩评论