Custom Right Click Short Cut Menu
-
Monday, March 05, 2012 10:57 AMHi Just a quick one. Wanting to desperately add additional right-click short cuts to those existing in MS Word 2010 using VB.NET. I found a similar method that works with bookmarks (http://msdn.microsoft.com/en-us/library/47cs5caa.aspx). I thought that I could add bookmark sections where required and it works, but unfortunately removes all the default short cuts (ie. cut, paste, etc). The shorts will be mainly style/layout changers for text formatting (ie. change style to Normal, Font Increase etc). Many thanks in advance
All Replies
-
Monday, March 05, 2012 12:19 PM
Here's and article on Creating Popup Menus in All Versions of Excel and below is VBA example code for Word. Based on the articel, I wrote the Word VBA that will create either a custom pop-up menu or contextual menus.
The sub "AddToTextMenu" will place a custom control on the contextual menu that appears when you right click in a document.
I hope this helps
Option Explicit Public Const Mname As String = "MyPopUpMenu" Sub DeletePopUpMenu() ' Delete the popup menu if it already exists. On Error Resume Next Application.CommandBars(Mname).Delete On Error GoTo 0 End Sub Sub CreateDisplayPopUpMenu() ' Delete any existing popup menu. Call DeletePopUpMenu ' Create the popup menu. Call Custom_PopUpMenu_1 ' Display the popup menu. On Error Resume Next Application.CommandBars(Mname).ShowPopup On Error GoTo 0 End Sub Sub Custom_PopUpMenu_1() Dim MenuItem As CommandBarPopup ' Add the popup menu. With Application.CommandBars.Add(Name:=Mname, Position:=msoBarPopup, _ MenuBar:=False, Temporary:=True) ' First, add two buttons to the menu. With .Controls.Add(Type:=msoControlButton) .Caption = "Button 1" .FaceId = 71 .OnAction = "TestMacro" End With With .Controls.Add(Type:=msoControlButton) .Caption = "Button 2" .FaceId = 72 .OnAction = "TestMacro" End With ' Next, add a menu that contains two buttons. Set MenuItem = .Controls.Add(Type:=msoControlPopup) With MenuItem .Caption = "My Special Menu" With .Controls.Add(Type:=msoControlButton) .Caption = "Button 1 in menu" .FaceId = 71 .OnAction = "TestMacro" End With With .Controls.Add(Type:=msoControlButton) .Caption = "Button 2 in menu" .FaceId = 72 .OnAction = "TestMacro" End With End With ' Finally, add a single button. With .Controls.Add(Type:=msoControlButton) .Caption = "Button 3" .FaceId = 73 .OnAction = "TestMacro" End With End With End Sub Sub TestMacro() MsgBox "Hi there! Greetings from Omaha, Nebraska. What's a boy from Florida doing out here?" End Sub Sub AddToTextMenu() Dim ContextMenu As CommandBar ' First, delete the control to avoid duplicates. Call DeleteFromTextMenu ' Set Context Menu variable to point to the Text menu. Set ContextMenu = Application.CommandBars("Text") ' Add one custom button to the Text menu. With ContextMenu.Controls.Add(Type:=msoControlButton, Before:=1) .OnAction = "CreateDisplayPopUpMenu" .FaceId = 59 .Caption = "My Popup menu" .Tag = "My_Text_Control_Tag" End With End Sub Sub DeleteFromTextMenu() Dim ContextMenu As CommandBar Dim ctrl As CommandBarControl ' Set ContextMenu variable to point to the Textmenu. Set ContextMenu = Application.CommandBars("Text") ' Delete custom controls with the tag: My_Text_Control_Tag. For Each ctrl In ContextMenu.Controls If ctrl.Tag = "My_Text_Control_Tag" Then ctrl.Delete End If Next ctrl End SubKind Regards, Rich ... http://greatcirclelearning.com
-
Tuesday, March 06, 2012 3:10 AM
Thanks for the help Rich, all the code worked fine but the custom shortcut isn't available from the right-click... It will display the custom shortcut menu fine if I run the macros via a keyboard shortcut. Any advice?? Also on a similar theme, am I stuck with the .FaceId icons or can I load small 16x16 BMP with .Picture?? Thanks again.
-
Tuesday, March 06, 2012 8:50 AMModerator
Hi,
Actually, I think you missed the last part of the sample.
The "CreateDisplayPopUpMenu" procedure works for adding and showing a new shortcut menu
The "AddToCellMenu procedure" works for adding a command button for context menu which shows by right clicking, the OnAction event for the new created command button is "CreateDisplayPopUpMenu"
And at last, call the "AddToCellMenu" procedure in Workbook_Active event so that every time the workbook actives, Office application would add a command button for context menu, and click on the button, you would see the shortcut menu.
On the other hand, you have an another option: working with Ribbon XML, see:
http://msdn.microsoft.com/en-us/library/ee691832.aspx
I hope this helps.
Calvin Gao[MSFT]
MSDN Community Support | Feedback to us
-
Tuesday, March 06, 2012 10:09 AM
Hi Danger,
Hopefully Calvin's response has answered both of your questions. Using the Ribbon XML method will allow you to customize the icons.
Kind Regards, Rich ... http://greatcirclelearning.com
-
Tuesday, March 06, 2012 11:19 AM
Many thanks for you both, I'm building this template in VSTO and I've already added a Ribbon thru Ribbon (VD) so I was hoping to find a .Net work around for the icon aspect seeing that VSTO won't let me create Ribon2 using Ribbon (XML). I ended up using the ClickEventArgs as the action...
Private Sub showPopupMenu(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Word.ClickEventArgs) _
Handles MyBase.BeforeRightClick With e.Selection COMMANDbar.ShowPopup() End With End Sub
... which seems to be working fine thus far. I will do a test build and throw it to the monkeys tomorrow morning and find out where it breaks. Thanks again to you both.
Once I have things working fine, I will post the code for any interested parties.
- Edited by DangerDraper Tuesday, March 06, 2012 11:34 AM
-
Wednesday, March 07, 2012 7:29 AMModerator
Hi Danger,
What did you mean of "VSTO won't let me create Ribbon2 using Ribbon(XML)"?
If you don't know how to assign your own image to a certain as its icon, you can refer to this document for how to use getImage callback:
http://msdn.microsoft.com/en-us/library/aa338202(v=office.12).aspx (Loading Images category)
I look forward to hearing of you.
Calvin Gao[MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, March 07, 2012 12:14 PM
Sorry Calvin, it was late at night when I wrote that reply. I'm using VS Pro 2010 not VSTO to create this template and I've already created several customisations; a Ribbon, Form and an ActionPane all in VB.Net.
I thought that VS would let be create another Ribbon (Ribbon2) using the "Ribbon (XML)" template but I get the following error... "Error HRESULT E_FAIL has been returned from a call to a COM component" This fault could also be my software, as I was getting this wierd instance where the Close command would finish a test build of an application but still be looping through the debug mode.
I'm really trying to keep as much of this template code in VB.Net rather than XML, at least until I improve my knowledge base further. Once I'm more confident then I'll branch out again into another language.
- Edited by DangerDraper Wednesday, March 07, 2012 12:15 PM


