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

8.3k views Asked by At

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

4

There are 4 answers

2
Nikhil On BEST ANSWER

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);
}
0
Marcus On

You need to extend the correct contextmenu. The following link describes in words (no source code) how this can be achieved:

Shared Addin using Word

Maybe this Link might help a little with the coding. I haven't tried it out myself, but it might point into the right direction.

Good luck! :)

Edit:

Does it have to be the ribbon style context menu or would a button within the normal context menu be enough? In case the normal menu would be ok, you might use this way (C#):

 Microsoft.Office.Core.CommandBar cb = this.Application.CommandBars["Text"];

 Office.CommandBarControl newButton = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, missing);  
 newButton.Caption = "Test";
 newButton.Visible = true;
 newButton.Enabled = true;

You can do this with VSTO, I'm not so sure if it works exactly the same way with the shared Add-In technology, but maybe it does help ;)

2
Brandon On

From MSDN -

You cannot modify the Mini toolbar programmatically.

a little over halfway down the doc. Search on mini toolbar.

Edit: The popup you have circled in the image above doesn't appear on right-click, it appears on highlight. The context menu (below the selected text) could have your custom functionality, but not in the mini toolbar.

0
jle On

http://groups.google.com/group/microsoft.public.word.docmanagement/browse_thread/thread/cf55d996b3f51a06/65b2bad22e2a3583?lnk=st&q=Removing+Items+from+Word+2007 is how to do it in VBA. It is very similar using COM and probably creating a word add-in (I have not tried it though) You basically need to find the context menu control and add an item to it (your function).