Word VSTO 插件 - 单击事件未触发?
Word VSTO addin - Click event not firing?
我正在为 Word 编写我的第一个 VSTO AddIn,我已经设法将一个按钮添加到 "Track Changes" 上下文菜单,但是我无法调用它来调用我的点击 handler.I 可以看到按钮在那里,但点击它什么也没做 - 我从来没有进入 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 的更多信息:
我正在为 Word 编写我的第一个 VSTO AddIn,我已经设法将一个按钮添加到 "Track Changes" 上下文菜单,但是我无法调用它来调用我的点击 handler.I 可以看到按钮在那里,但点击它什么也没做 - 我从来没有进入 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 的更多信息: