VBA code example for Word custom right-click menu

Source: Internet
Author: User
Tags reset

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.