Word VSTO插件-单击“事件未触发”

本文关键字:事件未触发 事件 VSTO 插件 单击 Word | 更新日期: 2023-09-27 18:08:08

我正在为Word编写我的第一个VSTO AddIn,我已经设法在"Track Changes"上下文菜单中添加了一个按钮,但我无法让它调用我的单击处理程序。我可以看到按钮在那里,但点击它做什么-我从来没有进入ButtonClick,也没有例外。我试过设置Enabled为真,Visible为真,但无济于事。

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 VSTO插件-单击“事件未触发”

命令栏已弃用。从Word 2007开始,使用Ribbon UI(又名Fluent UI)代替。在以下文章中阅读更多关于Fluent UI的信息:

  • Office 2010自定义上下文菜单

  • 为开发人员定制2007 Office Fluent功能区(1/3)

  • 为开发人员定制2007 Office Fluent功能区(2/3)
  • 为开发人员定制2007 Office Fluent功能区(3/3)