Word自定义右键菜单的VBA代码示例

时间:2023-12-14 04:07:24 

本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

标签:Word自定义右键菜单的VBA代码示例
0
投稿

猜你喜欢

  • Win7创建和修改登陆密码的方法

    2022-06-18 23:33:44
  • win8.1怎么降级win7? win8.1降回win7降级方法

    2023-09-29 21:34:50
  • 草图大师怎么安装 SketchUp2017软件的安装方法

    2023-03-19 08:14:25
  • Win7/Win8.1升级到Win10提示80240020错误的解决方法

    2022-09-08 01:56:00
  • windows7如何切换窗口?windows7切换窗口的方式

    2023-07-28 16:14:07
  • excel怎么设计三维立体的饼图?

    2022-07-18 04:45:56
  • word2010怎么绘制陶瓷纹理效果

    2023-11-10 12:05:17
  • 安装win7系统后有必要做的五项系统优化

    2023-05-25 04:57:58
  • IVMS-8800客户端使用说明书

    2023-01-23 18:47:26
  • 电脑有两个内存条槽,我可以再买一个插上去吗?要注意什么?

    2023-03-24 10:04:51
  • win7系统局域网共享文件密码删除的方法

    2023-06-13 04:04:21
  • excel用函数取整进行四舍五入的教程

    2022-04-13 14:27:34
  • 教大家电脑重装win10系统后如何设置硬盘密码保护

    2022-12-12 11:49:03
  • PPT制作撕纸效果教程

    2023-04-11 09:06:06
  • Word2007:制作精美文本框攻略

    2023-08-25 01:35:44
  • 五个Word的实用技巧方法

    2023-04-23 12:16:59
  • Win10系统“initpki.dll”加载失败怎么办?Win10“initpki.dll”加载失败的解决方法

    2023-04-01 06:36:15
  • 如何预约 Apple Store 的免费课程?

    2022-06-19 12:30:03
  • Win10笔记本/平板屏幕右下角的电池图标不见了怎么办?

    2023-08-19 20:12:04
  • 共享磁盘是什么 Win7系统设置共享磁盘的方法

    2022-01-19 10:16:53
  • asp之家 电脑教程 m.aspxhome.com