Introduction to the features of this word's VBA code: Add a custom command to the middle of the right-click Text menu (the menu that appears when you right-click the text), and perform the appropriate procedure.
Private Sub Document_Close ()
On Error Resume Next
Application.CommandBars ("Text"). Controls ("Test"). Delete ' Restore original menu
End Sub
Private Sub Document_Open ()
Dim Half as Byte
On Error Resume Next
Dim Newbutton as CommandBarButton
Application.CommandBars ("Text"). Controls ("Test"). Delete ' Preventative deletion
Half = Int (Application.CommandBars ("Text"). CONTROLS.COUNT/2) ' Middle position
Set Newbutton = Application.CommandBars ("Text"). Controls.Add (Type:=msocontrolbutton, Before:=half)
With Newbutton
. Caption = "Test" command name
. FaceId = 100 ' FaceId of the command
. Visible = True ' visible
. OnAction = "MySub" ' specifies the name of the response procedure
End With
End Sub
Sub mysub ()
MsgBox "It" A Test for CommandBars ("" Text "")! ", vbOKOnly + vbinformation
End Sub
Sub comreset () ' Reset right-click menu, restore default settings completely
Application.CommandBars ("Text"). Reset
End Sub
The build has CommandBars ("Toolbar list") or CommandBars ("View"). Controls (&t) in the form of a command button:
Private Sub Document_Close ()
On Error Resume Next
Application.CommandBars ("Text"). Controls ("New menu"). Delete ' Restore original menu
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 ' Preventative deletion
Half = Int (Application.CommandBars ("Text"). CONTROLS.COUNT/2) ' Middle position
Set Newbutton = Application.CommandBars ("Text"). Controls.Add (Type:=msocontrolpopup, Before:=half)
With Newbutton ' This is a pop-up menu with a small triangular type on the right.
. Caption = "New Menu" command name
. Visible = True ' visible
End With
For i = 1 to 4 ' new four subcommand, batch generation
StrName = "Menu" & I
Set Menuadd = NEWBUTTON.CONTROLS.ADD (Type:=msocontrolbutton)
With Menuadd
. Caption = StrName
. OnAction = "MySub"
. state = msoButtonDown ' with checked command button
. Visible = True
End With
Next
End Sub
Sub mysub ()
Dim Actiontag as String
Actioncap = CommandBars.ActionControl.Caption
MsgBox Actioncap
Select Case Actiontag
' to differentiate each command and execute the specified procedure
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 () ' Reset right-click menu, restore default settings completely
Application.CommandBars ("Text"). Reset
End Sub
The following are common ways to disable commands and shortcut keys and save paths, and it is more convenient to use the Modify Word command.
Sub Example ()
' Save custom menu bar toolbar or custom keyboard changes in the active document
Application.customizationcontext = ActiveDocument
' Use CommandBars (Name). Controls (Caption) to position the button with uniqueness
Application.CommandBars ("Standard"). Controls ("Open (&o) ..."). Enabled = False ' TRUE
' Use to locate the button, not too intuitive, easy to adjust the position of the command interference
Application.CommandBars ("Standard"). Controls (2). Enabled = True ' False
' Use FindControl (id:=) to locate the button, unique, and can loop, function multiple this button command
Application.CommandBars.FindControl (id:=23). Enabled = True ' False
' Use CommandBars (Index). Controls (Index) to locate the button, intuitive, but subject to the adjusted command position interference
Application.CommandBars (1). Controls (2). Enabled = False ' True
End Sub
Sub FileOpen () can disable commands with shortcut keys
MsgBox "This is to modify Word command/Open file"
End Sub
Sub Sample () will reassign or modify the Ctrl+o shortcut key and save it in the current document
CustomizationContext = ActiveDocument
Keybindings.add Keycode:=buildkeycode (wdKeyControl, Wdkeyo), _
Keycategory:=wdkeycategorymacro, command:= "Nofileopen"
End Sub
Sub Nofileopen ()
MsgBox "This are only a test!"
End Sub