Search code examples
c#word-2007shared-addin

MS Word Plugin, Adding a button which pops up on right click on selected text


I am working on a shared addin for MS Word 2007. I would like to add a button which pops up when selected text is right clicked. The attached snapshot should make this clear.

Currently, the user has to select the text and then click a button on a custom control. It would be a lot easier if after selecting the text, s/he could right click it and press the relevant button in the popup.

alt text


Solution

  • Here is how this can be done...

    Microsoft.Office.Core.CommandBar cellbar = diff.CommandBars["Text"];
    Microsoft.Office.Core.CommandBarButton button = (Microsoft.Office.Core.CommandBarButton)cellbar.FindControl(Microsoft.Office.Core.MsoControlType.msoControlButton, 0, "MYRIGHTCLICKMENU", Missing.Value, Missing.Value);
    if (button == null)
    {
       // add the button
       button = (Microsoft.Office.Core.CommandBarButton)cellbar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, Missing.Value, Missing.Value, cellbar.Controls.Count + 1, true);
       button.Caption = "My Right Click Menu Item";
       button.BeginGroup = true;
       button.Tag = "MYRIGHTCLICKMENU";
       button.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(MyButton_Click);
    }