Word自定义右键菜单的VBA代码示例
本Word的VBA代码的功能简介:在右键文本菜单的中部位置(相当于右击文本时出现的菜单),添加一个自定义命令,并执行相应过程。
Private Sub Document_Close()
On Error Resume Next
Application.CommandBars("Text").Controls("Test").Delete ’恢复原有菜单
End Sub
Private Sub Document_Open()
Dim Half As Byte
On Error Resume Next
Dim NewButton As CommandBarButton
Application.CommandBars("Text").Controls("Test").Delete ‘预防性删除
Half = Int(Application.CommandBars("Text").Controls.Count / 2) ‘中间位置
Set NewButton = Application.CommandBars("Text").Controls.Add(Type:=msoControlButton, Before:=Half)
With NewButton
.Caption = "Test" ’命令名称
.FaceId = 100 ’命令的FaceId
.Visible = True ’可见
.OnAction = "MySub" ‘指定响应过程名
End With
End Sub
Sub MySub()
MsgBox "It’s A Test For CommandBars(""Text"")!", vbOKOnly + vbInformation
End Sub
Sub ComReset() ‘重新设置右键菜单,彻底恢复默认设置
Application.CommandBars("Text").Reset
End Sub
生成具有Commandbars(“Toolbar list”)或者当于CommandBars("View").Controls("工具栏(&T)")中的命令按钮形式:
Private Sub Document_Close()
On Error Resume Next
Application.CommandBars("Text").Controls("New Menu").Delete ’恢复原有菜单
End Sub
Private Sub Document_Open()
Dim i As Byte, Half As Byte, strName As String, NewButton As CommandBarPopup
Dim MenuAdd As CommandBarButton
On Error Resume Next
Application.CommandBars("Text").Controls("New Menu").Delete ‘预防性删除
Half = Int(Application.CommandBars("Text").Controls.Count / 2) ‘中间位置
Set NewButton = Application.CommandBars("Text").Controls.Add(Type:=msoControlPopup, Before:=Half)
With NewButton ’这是弹出式菜单即右边带有小三角型的
.Caption = "New Menu" ’命令名称
.Visible = True ‘可见
End With
For i = 1 To 4 ’新建四个子命令,批量生成
strName = "Menu" & i
Set MenuAdd = NewButton.Controls.Add(Type:=msoControlButton)
With MenuAdd
.Caption = strName
.OnAction = "MySub"
.State = msoButtonDown ‘带勾选的命令按钮
.Visible = True
End With
Next
End Sub
Sub MySub()
Dim ActionTag As String
ActionCap = CommandBars.ActionControl.Caption
MsgBox ActionCap
Select Case ActionTag
’以此来区分各个命令并执行指定过程
End Select
With Application.CommandBars("Text").Controls("New Menu")
If .Controls(ActionCap).State = msoButtonDown Then
MsgBox "It’s A Test!", vbOKOnly + vbInformation
.Controls(ActionCap).State = msoButtonUp
Else
.Controls(ActionCap).State = msoButtonDown
End If
End With
End Sub
Sub ComReset() ‘重新设置右键菜单,彻底恢复默认设置
Application.CommandBars("Text").Reset
End Sub
以下为禁用命令和快捷键的常用方式与保存路径,提倡使用修改WORD命令更方便。
Sub Example()
‘将自定义菜单栏工具栏或者自定义键盘的改变保存于活动文档中
Application.CustomizationContext = ActiveDocument
‘利用CommandBars(Name).Controls(Caption)来定位按钮,具有唯一性
Application.CommandBars("Standard").Controls("打开(&O)...").Enabled = False ‘TRUE
‘ 利用来定位按钮,不太直观,容易受调整后的命令位置干扰
Application.CommandBars("Standard").Controls(2).Enabled = True ‘False
‘利用Findcontrol(ID:=)来定位按钮,具有唯一性,并可循环,作用多个此按钮命令
Application.CommandBars.FindControl(ID:=23).Enabled = True ‘False
‘利用CommandBars(Index).Controls(Index)来定位按钮,直观,但受调整后的命令位置干扰
Application.CommandBars(1).Controls(2).Enabled = False ‘True
End Sub
Sub FileOpen() ‘可以将命令与快捷键一并禁用
MsgBox "这是修改WORD命令/打开文件"
End Sub
Sub Sample() ’将 CTRL+O快捷键重新分配或者修改并保存于当前文档中
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyO), _
KeyCategory:=wdKeyCategoryMacro, Command:="NoFileOpen"
End Sub
Sub NoFileOpen()
MsgBox "This is only a test!"
End Sub
您可能感兴趣的文章
- 11-262010 Office密钥过期怎么办?Office2010产品激活失败的解决方法
- 11-26office2010产品密钥有哪些?最新office2010激活密钥汇总
- 11-26office2007密钥哪里找?office2007永久密钥序列号
- 11-26Office2016怎么安装?Office2016安装激活教程
- 11-26Office2010最新激活密钥有哪些?Office2010激活密钥分享大全
- 11-26求Office2013密钥?2025最新Office2013激活密钥
- 11-26Office2013电话怎么激活?Office2013永久激活方法
- 11-26office2016打开卡顿怎么办?office2016打开缓慢的解决技巧
- 11-26Office 2010中文版怎么转换为英文版?
- 11-26office 2010激活错误代码0X8007000D如何解决?


阅读排行
- 12010 Office密钥过期怎么办?Office2010产品激活失败的解决方法
- 2office2010产品密钥有哪些?最新office2010激活密钥汇总
- 3office2007密钥哪里找?office2007永久密钥序列号
- 4Office2016怎么安装?Office2016安装激活教程
- 5Office2010最新激活密钥有哪些?Office2010激活密钥分享大全
- 6求Office2013密钥?2025最新Office2013激活密钥
- 7Office2013电话怎么激活?Office2013永久激活方法
- 8office2016打开卡顿怎么办?office2016打开缓慢的解决技巧
- 9Office 2010中文版怎么转换为英文版?
- 10office 2010激活错误代码0X8007000D如何解决?
推荐教程
- 07-012025最新office2010永久激活码免费分享,附office 2010激活工具
- 07-01怎么免费获取2025最新office2016专业增强版永久激活密钥/序列号
- 07-01office365怎么永久激活?2025.5最新office,office365序列号推荐
- 06-25office2021年最新永久有效正版激活密钥免费分享
- 06-13Office 2024激活工具,2025年Office2024激活工具及激活密钥分享
- 05-12Microsoft office2019产品激活密钥(100%永久激活)
- 07-01office2021专业增强版2021激活密钥2023密钥永久激活 万能office202
- 06-17经验分享Win11激活密钥怎么获取(win11专业版激活密钥)
- 07-01office 2021激活码/序列号推荐,office2021附激活工具
- 06-25Office2021激活密钥永久最新分享




