C#、Word 2003 addIn 和工具栏按钮事件
我正在尝试为 Word 2003 编写应用程序级加载项。 该插件在新的命令栏上添加了一个按钮 - 单击该按钮可保存活动文档,然后执行一些其他操作。当我启动 Word 2003,然后单击命令栏按钮时,一切正常。 但是,如果我启动 Word 2003,通过单击“标准”工具栏上的工具栏按钮“新文档”打开一个新的 Word 窗口,然后单击我的命令栏按钮,结果发现没有执行任何操作。看来我在新打开的窗口上的工具栏按钮没有分配“onclick”事件处理程序。您知道如何解决这个问题吗?
我的加载项代码基于以下代码:
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);
}
问候 詹克
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);
}
Regards
JanK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您的“加载项”未加载,但您的工具栏正在保留。您是否已将“加载项”放置在 Word 的启动位置之一?
有关 Word 2003 或 Word 2007 中模板位置的常见问题,
问题 3:我的 Word 加载项文件保存在哪里?
http://support.microsoft.com/kb/826867。
加载 Word 加载项,第二个项目符号 http:// /msdn.microsoft.com/en-us/library/aa165426(office.10).aspx
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
还没有在 Word 中完成此操作,但我相信在 Outlook 中,我通过侦听 NewWindow 事件(在 Outlook 中称为资源管理器和检查器)并在创建新窗口时重新添加按钮(并使用“true”)来使其工作作为 commandBar.Controls.Add 中的最后一个参数,使按钮“临时”,这样你就不会得到两个按钮)。
PS我同意它应该像你期望的那样工作,并且不确定为什么需要它(或者如果“temp”是“false”它应该如何工作)。
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").
我遇到了同样的问题,并通过在按钮上设置 Tag 属性来解决它。这似乎是设计使然。
http://support.microsoft.com/kb/826931
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