Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
As many Word users are painfully aware, the masters at Microsoft radically changed the user interface with Word 2007. Making matters worse they practically eliminated the ability to change and customize the interface without resorting to third party add-ins or doing your own programming.
While alleviating the problem some with the introduction of limited customization capability in Word 2010, this Microsoft Word Help & Tips page can still be beneficial and will show you you you can use VBA to customize the Word shortcut (aka right-click) menus programmatically.
The method presented here isn't new. The ability to customize the Word menus and toolbars programmatically is available in earlier versions of Word. However, most users never had a need for it since these menus and toolbars were easily customized through the user interface.
As I can't cover every customization possibility, I am going to present a basic example which will demonstrate:
For a recent practical example, see Word AutoCorrect Restored
Note: The "Text" shortcut menu is probably the most prevalent when users right click their mouse. However, as the shortcut menus are contextual, there are situations when it will be replaced by another menu and not be displayed.
The illustration below shows the PopUp menu and new commandbar controls that I have added at the top of the "Text" shortcut menu.
The VBA code required to build these particular controls is shown below:
Option Explicit Dim oPopUp As CommandBarPopup Dim oCtr As CommandBarControl Sub BuildControls() Dim oBtn As CommandBarButton 'Make changes to the Normal template CustomizationContext = NormalTemplate 'Prevent double customization Set oPopup = CommandBars.FindControl(Tag:="custPopup") If Not oPopup Is Nothing Then GoTo Add_Individual 'Add PopUp menu control to the top of the "Text" short-cut menu Set oPopUp = CommandBars("Text").Controls.Add(msoControlPopup, , , 1) With oPopUp .Caption = "My Very Own Menu" .Tag = "custPopup" .BeginGroup = True End With 'Add controls to the PopUp menu Set oBtn = oPopUp.Controls.Add(msoControlButton) With oBtn .Caption = "My Number 1 Macro" .FaceId = 71 .Style = msoButtonIconAndCaption 'Identify the module and procedure to run .OnAction = "MySCMacros.RunMyFavMacro" End With Set oBtn = Nothing 'Add a Builtin command using ID 1589 (Co&mments) Set oBtn = oPopUp.Controls.Add(msoControlButton, 1589) Set oBtn = Nothing 'Add the third button Set oBtn = oPopUp.Controls.Add(msoControlButton) With oBtn .Caption = "AutoText Complete" .FaceId = 940 .Style = msoButtonIconAndCaption .OnAction = "MySCMacros.MyInsertAutoText" End With Set oBtn = Nothing Add_Individual: 'Or add individual commands directly to menu Set oBtn = CommandBars.FindControl(Tag:="custCmdBtn") If Not oBtn Is Nothing Then Exit Sub 'Add control using built-in ID 758 (Boo&kmarks...) Set oBtn = Application.CommandBars("Text").Controls.Add(msoControlButton, 758, , 2) oBtn.Tag = "custCmdBtn" If MsgBox("This action caused a change to your Normal template." _ & vbCr + vbCr & "Recommend you save those changes now.", vbInformation + vbOKCancel, _ "Save Changes") = vbOK Then NormalTemplate.Save End If Set oPopUp = Nothing Set oBtn = Nothing lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
Each of the custom controls (i.e., controls that aren't assigned a built-in Id) references an "On Action" procedure. These are the procedures set to run when the user clicks on the control. The two I used in are shown below. The first control simply shows you how you can set the control to run one of your custom macros. The second one mimics pressing the F3 key to complete an AutoText/Building Block entry.
Sub RunMyFavMacro() MsgBox "Hello " & Application.UserName & ", this could be the results of your code." lbl_Exit: Exit Sub End Sub Sub MyInsertAutoText() On Error GoTo Err_Handler Application.Run MacroName:="AutoText" Exit Sub Err_Handler: Beep Application.StatusBar = "The specified text is not a valid AutoText\BuildingBlock name." End Sub
The menu customization is saved in your normal template file. Once the controls are added and your normal template is saved you really have no further need of the code that builds the controls. However, if after building the custom controls you should want to edit them then you first have to delete the custom controls with VBA and run a your own revised version of the BuildControl procedure to create new controls. The code to remove the custom controls is shown below:
Sub RemoveContentMenuItem() 'Make command bar changes in Normal.dotm CustomizationContext = NormalTemplate On Error GoTo Err_Handler Set oPopUp = CommandBars("Text").Controls("My Very Own Menu") 'Delete individual commands on the PopUp menu For Each oCtr In oPopUp.Controls oCtr.Delete Next 'Delete the PopUp itself oPopUp.Delete 'Delete individual custom commands on the Text menu For Each oCtr In Application.CommandBars("Text").Controls If oCtr.Caption = "Boo&kmark..." Then oCtr.Delete Exit For End If Next oCtr If MsgBox("This action caused a change to your Normal template." _ & vbCr + vbCr & "Recommend you save those changes now.", vbInformation + vbOKCancel, _ "Save Changes") = vbOK Then NormalTemplate.Save End If Set oPopUp = Nothing Set oCtr = Nothing Exit Sub Err_Handler: End Sub
You make these procedures available to your Word application by putting the code in a template AddIn and placing the AddIn in your Word Startup directory.
I added some basic Ribbon customization to the add-in I created. When the add-in loads a new group with a couple of command buttons are placed on the Add-Ins tab. You can use these commands to quickly and easily fire the procedures that build your custom menu controls or delete those controls. See Customize the Ribbon (It doesn't take Rocket Science) for my tips on customizing the ribbon.
So what is FaceId anyway? You might have noticed that I used the FaceId property of the commandbar controls in several instances. The FaceId defines the look, not the function, of a commandbar button. You can select from several hundred FaceId numbers which are referenced in the FaceId.dot template that you can download here: FaceId.dot
A control's "Id" on the other hand determines the built-in action for that control. In the example shown here, I used the built-in Id "1589" for the "Insert Comment" control and "758" for the "Add Bookmark" control. These are the same control Ids as used for menu Insert>Comment and Insert>Bookmark commands in Word 2003. I have included a list of control Ids here: Control Ids.
There is an inconsistent issue with this AddIn that I haven't been able to eliminate. Sometimes when you run the BuildControls procedure for the first time (either initially or for after making modifications) the controls are either not built or don't function properly. Each time that this has happened, I have just deleted the controls and run the BuildControls procedure a second time and it works perfectly. As the little icon indicates, I have given up.
For more on commandbar customization see the Word MVP FAQs: Assigning custom button images to your toolbar and menu buttons and How to use VBA to set a custom graphic on a toolbar button.
Prompted by activity in various Word support forum's, I've put together a method for restoring the "AutoCorrect" command on the spelling error context menu in Word 2013. While not perfect, if you can tolerate a slight occasional flicker of the mouse pointer and and have come to rely on this feature then it might ease your pain. You can download the template here and put it in Word's startup folder or load it manually. Word2013 AutoCorrect Restored
Note: To see the red squiggly line under misspellings, ensure the option "Check spelling as you type" is checked in File>Options>Proofing>When correcting spelling and grammar in Word.
That's it! I hope you have found this tips page useful and informative. You can download the template add-in I used in this Microsoft Word Help & Microsoft Word Tips page here: Customize Shortcut Menu.
Do you want to make a payment for consulting work or donate to help support this site?
PayPal is a safe, easy way to pay online.
Use the appropriate currency "Donate" button to make a payment or donation.