[英]Word VSTO addin - Click event not firing?
我正在编写我的第一个VSTO Word的VSTO插件,我设法在“跟踪更改”上下文菜单中添加了一个按钮,但是我无法通过它调用我的点击处理程序。我可以在此处看到该按钮,但是单击它却无济于事-我从不进入ButtonClick
,也没有例外。 我尝试将“ Enabled
设置为true,将“ Visible
设置为true,但无济于事。
public partial class ThisAddIn
{
Word.Application application;
string insertText = "INSERT!!";
Office.CommandBarButton acceptButton;
Office.CommandBar commandBar;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
application = this.Application;
application.WindowBeforeRightClick +=
new Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(application_WindowBeforeRightClick);
application.DocumentOpen +=
new Word.ApplicationEvents4_DocumentOpenEventHandler(WorkWithDocument);
((Word.ApplicationEvents4_Event)this.Application).NewDocument +=
new Word.ApplicationEvents4_NewDocumentEventHandler(WorkWithDocument);
}
private void WorkWithDocument(Microsoft.Office.Interop.Word.Document Doc)
{
try
{
application.CustomizationContext = application.ActiveDocument;
commandBar = application.CommandBars["Track Changes"];
acceptButton = (Office.CommandBarButton)commandBar.Controls.Add(
Office.MsoControlType.msoControlButton);
acceptButton.accName = insertText;
acceptButton.Caption = insertText;
acceptButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);
}
catch (Exception ex)
{
Debug.Print(ex.StackTrace);
// Handle exception if for some reason the document is not available.
}
}
// Handles the event when a button on the new toolbar is clicked.
private void ButtonClick(Office.CommandBarButton ctrl, ref bool cancel)
{
try
{
Debug.Print("You clicked: " + ctrl.Caption);
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}
...
不建议使用命令栏。 从Word 2007开始,将使用功能区UI(又称Fluent UI)。 在以下文章中阅读有关Fluent UI的更多信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.