2015-12-15 44 views
3

我試圖根據this的帖子在Microsoft Word中創建一個右鍵單擊菜單項。如何在Microsoft Office Word中添加菜單項

這裏是我的代碼:

private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     try 
     { 
      eventHandler = new _CommandBarButtonEvents_ClickEventHandler(MyButton_Click); 
      Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
      applicationObject.WindowBeforeRightClick += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick); 
     } 
     catch (Exception exception) 
     { 
      MessageBox.Show("Error: " + exception.Message); 
     } 
    } 

    void App_WindowBeforeRightClick(Microsoft.Office.Interop.Word.Selection Sel, ref bool Cancel) 
    { 
     try 
     { 
      this.AddItem(); 
     } 
     catch (Exception exception) 
     { 
      MessageBox.Show("Error: " + exception.Message); 
     } 

    } 
    private void AddItem() 
    { 
     Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
     CommandBarButton commandBarButton = applicationObject.CommandBars.FindControl(MsoControlType.msoControlButton, missing, "HELLO_TAG", missing) as CommandBarButton; 
     if (commandBarButton != null) 
     { 
      System.Diagnostics.Debug.WriteLine("Found button, attaching handler"); 
      commandBarButton.Click += eventHandler; 
      return; 
     } 
     CommandBar popupCommandBar = applicationObject.CommandBars["Text"]; 
     bool isFound = false; 
     foreach (object _object in popupCommandBar.Controls) 
     { 
      CommandBarButton _commandBarButton = _object as CommandBarButton; 
      if (_commandBarButton == null) continue; 
      if (_commandBarButton.Tag.Equals("HELLO_TAG")) 
      { 
       isFound = true; 
       System.Diagnostics.Debug.WriteLine("Found existing button. Will attach a handler."); 
       commandBarButton.Click += eventHandler; 
       break; 
      } 
     } 
     if (!isFound) 
     { 
      commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true); 
      System.Diagnostics.Debug.WriteLine("Created new button, adding handler"); 
      commandBarButton.Click += eventHandler; 
      commandBarButton.Caption = "h5"; 
      commandBarButton.FaceId = 356; 
      commandBarButton.Tag = "HELLO_TAG"; 
      commandBarButton.BeginGroup = true; 
     } 
    } 

    private void RemoveItem() 
    { 
     Word.Application applicationObject = Globals.ThisAddIn.Application as Word.Application; 
     CommandBar popupCommandBar = applicationObject.CommandBars["Text"]; 
     foreach (object _object in popupCommandBar.Controls) 
     { 
      CommandBarButton commandBarButton = _object as CommandBarButton; 
      if (commandBarButton == null) continue; 
      if (commandBarButton.Tag.Equals("HELLO_TAG")) 
      { 
       popupCommandBar.Reset(); 
      } 
     } 
    } 
    private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
    { 
     Word.Application App = Globals.ThisAddIn.Application as Word.Application; 
     App.WindowBeforeRightClick -= new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(App_WindowBeforeRightClick); 

    } 

    #region VSTO generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InternalStartup() 
    { 
     this.Startup += new System.EventHandler(ThisAddIn_Startup); 
     this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 
    } 
    #endregion 
    //Event Handler for the button click 

    private void MyButton_Click(CommandBarButton cmdBarbutton, ref bool cancel) 
    { 
     System.Windows.Forms.MessageBox.Show("Hello !!! Happy Programming", "l19 !!!"); 
     RemoveItem(); 
    } 
} 

}

,當我右鍵點擊一個字母結果:

enter image description here

但有一張桌子,我不能做到這一點。退房的屏幕截圖,看看我的意思是:

enter image description here

我無法添加項目菜單,當我用鼠標右鍵單擊MS Word中的表格。請幫幫我。 謝謝!

對不起我的英語,...

+0

https://msdn.microsoft.com/zh-cn/office/aa905340.aspx – MickyD

+0

你是什麼意思,「用表我不能做」?實際發生了什麼,你期望什麼?你屏幕截圖中的箭頭是什麼意思? –

+0

我想在右鍵菜單中添加一個菜單項。用桌子我不能這樣做。我想知道在ms word 2013中做的代碼。 – Quang

回答

2

Word維護多個上下文菜單。您可以通過在Application.CommandBars其位置枚舉所有CommandBar對象看到它們是msoBarPopup

foreach (var commandBar in applicationObject.CommandBars.OfType<CommandBar>() 
           .Where(cb => cb.Position == MsoBarPosition.msoBarPopup)) 
{ 
    Debug.WriteLine(commandBar.Name); 
} 

被鏈接的示例中使用的命令欄是一個名爲「文本」,這一條涉及上下文當您右鍵單擊段落文本中的某個位置時彈出菜單。

然而,添加的東西你有你的按鈕添加到相應的表相關的上下文菜單表的上下文菜單。表具有取決於選擇什麼不同的上下文菜單當你點擊:

  • applicationObject.CommandBars [ 「表」]
  • applicationObject.CommandBars [ 「表中的文本」]
  • applicationObject.CommandBars [「表細胞 「]
  • applicationObject.CommandBars [」 表格標題 「]
  • applicationObject.CommandBars [」 表列出 「]
  • applicationObject.CommandBars [」 表圖片「]

所以我建議你提取一個方法,將一個按鈕添加到CommandBar然後你調用所有的命令欄,你想添加按鈕的方法。像下面這樣:

private void AddButton(CommandBar popupCommandBar) 
{ 
    bool isFound = false; 
    foreach (var commandBarButton in popupCommandBar.Controls.OfType<CommandBarButton>()) 
    { 
     if (commandBarButton.Tag.Equals("HELLO_TAG")) 
     { 
      isFound = true; 
      Debug.WriteLine("Found existing button. Will attach a handler."); 
      commandBarButton.Click += eventHandler; 
      break; 
     } 
    } 
    if (!isFound) 
    { 
     var commandBarButton = (CommandBarButton)popupCommandBar.Controls.Add 
      (MsoControlType.msoControlButton, missing, missing, missing, true); 
     Debug.WriteLine("Created new button, adding handler"); 
     commandBarButton.Click += eventHandler; 
     commandBarButton.Caption = "Hello !!!"; 
     commandBarButton.FaceId = 356; 
     commandBarButton.Tag = "HELLO_TAG"; 
     commandBarButton.BeginGroup = true; 
    } 
} 

// add the button to the context menus that you need to support 
AddButton(applicationObject.CommandBars["Text"]); 
AddButton(applicationObject.CommandBars["Table Text"]); 
AddButton(applicationObject.CommandBars["Table Cells"]); 
1

正如德克表示,你需要點擊編輯鏈接你原來的問題下,貼在你的「答案」的信息在它的結束,然後刪除了「答案」 - 這是不是答案...

我的答案基於您提供的附加信息。這顯然是一個VSTO應用程序級插件。因此,對於Office 2013,您需要使用功能區XML創建自定義菜單。這不能使用功能區設計器來完成,所以如果您已經有功能區設計器,則需要將其轉換爲功能區XML。你會發現如何將VSTO文檔中這樣做的一篇文章: https://msdn.microsoft.com/en-us/library/aa942866.aspx

如何使用功能區XML定製上下文菜單的信息可以在這個MSDN文章中找到: https://msdn.microsoft.com/en-us/library/ee691832(v=office.14)

總結:您需要爲Ribbon XML添加<contextMenus>元素,併爲每個要添加或更改的菜單項添加<contextMenu>元素。元素的idMso屬性指定WHICH上下文菜單。您可以在Microsoft網站的下載中找到ControlIds列表(idMso的值): https://www.microsoft.com/en-us/download/details.aspx?id=36798

FWIW該上下文菜單的ControlId可能是ContextMenuTextTable。

+0

對不起, 我還沒有做,幫幫我。使用功能區我仍然無法添加項目菜單,當我右鍵單擊ms字表時 – Quang

+0

單擊原始問題下的「編輯」鏈接,並在結尾處粘貼添加到的功能區XML你的項目。另外,請確定表格單元格中您嘗試右鍵單擊的內容的類型。單詞可以區分很多事情 - 例如是否標記了拼寫錯誤 - 並且爲所有可能性都有單獨的右鍵單擊菜單。您可能需要將控件添加到多個上下文菜單中,以便在所有情況下都能顯示... –

相關問題