如何用vba给word文档的右键快捷菜单添加命令?

如下图所示:

当在word文档中单击鼠标右键时,为弹出的快捷菜单中添加一个自定义的命令“给加粗的字符添加括号”,可以使用以下的代码:

Sub CreateMenu()
      '命令栏
    Dim oCB As CommandBar
    '通用的命令栏控件
    Dim oCBC As CommandBarControl
    '命令按钮控件
    Dim oCBB As CommandBarButton
    '组合框控件
    Dim oCBCom As CommandBarComboBox
    '组合框控件
    Dim oCBCom1 As CommandBarComboBox
    '弹出式菜单
    Dim oCBPop As CommandBarPopup
    Set oCB = Word.Application.CommandBars("Text")
    With oCB
        .Reset
        Set oCBB = .Controls.Add(Before:=1)
        With oCBB
            .Caption = "给加粗的字符添加括号"
            .FaceId = 19
            .OnAction = "xyf"
        End With
    End With
End Sub
Sub xyf()
    MsgBox "测试成功"
End Sub

这里特别要注意的是,给word 文档的右键快捷菜单添加自定义命令对应的命令栏是

Word.Application.CommandBars("Text")

 

       

发表评论