实际上有许多这样的上下文菜单,最常出现的是文本上下文菜单。
这些菜单可以编辑,但不能直接通过 Word 界面进行编辑。它们可以用 XML 和使用 vba 进行编辑。此类编辑可以保存在 Normal.dotm 模板或全局模板或其他文档模板(甚至文档)中。
请参阅 Greg Maxey 的页面。自定义快捷菜单这是该页面上用于向菜单添加命令的代码。
Option Explicit
' Greg Maxey
' https://gregmaxey.com/word_tip_pages/customize_shortcut_menu.html
Dim oPopUp As CommandBarPopup
Dim oCtr As CommandBarControl
'
Sub BuildControls()
' Greg Maxey
' https://gregmaxey.com/word_tip_pages/customize_shortcut_menu.html
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
Run Code Online (Sandbox Code Playgroud)
这些添加的控件运行宏和内置命令。
这是两个示例宏。
Sub RunMyFavMacro()
MsgBox "Hello " & Application.UserName & ", this could be the results of your code."
lbl_Exit:
Exit Sub
End Sub
Sub MyInsertAutoText()
On Error GoTo Err_Handler
'Mimics pressing F3 key to create an autotext/buildingblock
Application.Run MacroName:="AutoText"
Exit Sub
Err_Handler:
Beep
Application.StatusBar = "The specified text is not a valid AutoText\BuildingBlock name."
End Sub
Run Code Online (Sandbox Code Playgroud)
他接着解释了 FaceID、控制 ID 和自定义图标的使用。
Ron DeBruin 在他的 Excel 上下文菜单页面底部提供了一个指向来自 Microsoft 的小插件的链接,该插件在菜单底部添加了每个上下文菜单的名称。以下是加载项处于活动状态的屏幕截图。
即使它说它适用于 Office 2010,它也适用于 Office 2019。
归档时间: |
|
查看次数: |
275 次 |
最近记录: |