PDA

View Full Version : Customize Shortcut Menus for Word



lamnbj
03-11-2022, 07:40 AM
I customize the right-click menu like this

Option Explicit
Dim oPopUp As CommandBarPopup
Dim oCtr As CommandBarControl
Sub BuildControls()
Dim oBtn As CommandBarButton
'Make changes to the Add-In template
CustomizationContext = ThisDocument.AttachedTemplate
'Prevent double customization
Set oPopup = CommandBars.FindControl(Tag:="custPopup")
If Not oPopup Is Nothing Then GoTo Add_Individual
'Add PopUp menu control to the top of the "Text" short-cut menu
Set oPopUp = CommandBars("Text").Controls.Add(msoControlPopup, , , 1)
With oPopUp
.Caption = "My Very Own Menu"
.Tag = "custPopup"
.BeginGroup = True
End With
'Add controls to the PopUp menu
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "My Number 1 Macro"
.FaceId = 71
.Style = msoButtonIconAndCaption
'Identify the module and procedure to run
.OnAction = "MySCMacros.RunMyFavMacro"
End With
Set oBtn = Nothing
'Add a Builtin command using ID 1589 (Co&mments)
Set oBtn = oPopUp.Controls.Add(msoControlButton, 1589)
Set oBtn = Nothing
'Add the third button
Set oBtn = oPopUp.Controls.Add(msoControlButton)
With oBtn
.Caption = "AutoText Complete"
.FaceId = 940
.Style = msoButtonIconAndCaption
.OnAction = "MySCMacros.MyInsertAutoText"
End With
Set oBtn = Nothing
Add_Individual:
'Or add individual commands directly to menu
Set oBtn = CommandBars.FindControl(Tag:="custCmdBtn")
If Not oBtn Is Nothing Then Exit Sub
'Add control using built-in ID 758 (Boo&kmarks...)
Set oBtn = Application.CommandBars("Text").Controls.Add(msoControlButton, 758, , 2)
oBtn.Tag = "custCmdBtn"
If MsgBox("This action caused a change to your Add-In template." _
& vbCr + vbCr & "Recommend you save those changes now.", vbInformation + vbOKCancel, _
"Save Changes") = vbOK Then
ThisDocument.Save
End If
Set oPopUp = Nothing
Set oBtn = Nothing
lbl_Exit:
Exit Sub
End Sub


When highlighting and selecting text, the re-added right click menu doesn't show up. Help me!

Chas Kenyon
03-11-2022, 10:55 AM
See Customize Text Shortcut Menu by Greg Maxey (http://gregmaxey.com/word_tip_pages/customize_shortcut_menu.html). If that is where you got your code, attribution would be a good thing.
As noted on that page, you can also use custom XML to modify the context menus, somewhat more easily.

Highlighting and selecting are different things.
When you select text you are still going to get the Text Context Menu.

There are a bunch of different context menus. I use the Add-In from this site to make sure of which one is being called. It gives the name at the bottom of the menu when it is called. https://www.add-in-express.com/creating-addins-blog/2015/04/15/creating-office-context-menu-addin/

Does the menu show up otherwise?

lamnbj
03-12-2022, 12:19 AM
Thanks you, but I mean when I select the object and then right click it won't show
29498

Aussiebear
03-12-2022, 12:49 AM
A very good piece of code by Greg Maxey, and it seems that lamnbj doesn't care enough to acknowledge this point.

Chas Kenyon
03-14-2022, 01:37 PM
Did you try using the Add-In linked to check and make sure which contextual menu is being displayed? It looks like you are selecting an equation, which could be giving you a different menu.

Again, if you started with code from someone else, that should be identified.