本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