C#、Word 2003 addIn 和工具栏按钮事件

发布于 2024-08-19 04:50:02 字数 1255 浏览 3 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裂开嘴轻声笑有多痛 2024-08-26 04:50:02

我怀疑您的“加载项”未加载,但您的工具栏正在保留。您是否已将“加载项”放置在 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

•Word 启动时自动执行,通过
将模板文件保存在Word中
计算机上的启动文件夹。这
该文件夹的默认路径是
C:\Windows\应用程序
数据\Microsoft\Word\启动;如果你是
使用用户配置文件,默认路径

C:\Windows\Profiles\用户名\应用程序
数据\微软\Word\启动。你可以
在“选项”对话框中更改此路径
盒子

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

九公里浅绿 2024-08-26 04:50:02

还没有在 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").

清风无影 2024-08-26 04:50:02

我遇到了同样的问题,并通过在按钮上设置 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文