|
As many Word users are painfully aware, the
masters at Microsoft radically changed the user interface with Word2007
and to make matters worse they practically eliminated the ability to
change and customize the interface without resorting to third party
AddIns or doing your own programming.
This Microsoft Word Help & Tips page will
show you how to use VBA to customize the Word2007 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. Since I can't cover
every customization possibility, I am going to present a basic example.
The example adds a new PopUp menu control to the "Text" shortcut menu
(that is the one that is probably presented most often when a user
right-clicks the mouse) and an independent commandbar button control.
I'll add three more commandbar button controls to the PopUp menu.
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
End Sub |
|
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 this example are show
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. |
Sub RunMyFavMacro()
MsgBox "Hello " & Application.UserName & ", this could be the results of
your code."
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 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 BuildConrol procedure shown above to create new
controls. The code to remove the custom controls is shown below: |
Sub RemoveContentMenuItem()
'Make command bar changes in Normal.dot
CustomizationContext = NormalTemplate
On Error GoTo Err_Handler
Set oPopUp = CommandBars("Text").Controls("My Very Own Menu")
'Delete individual commans 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. For more on Add-Ins and how
to load them, see the heading "Organizing Global Templates" at:
Organizing Your Macros
I added some very 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 Word2003.
I have included a list of control Ids here:
Control Ids |
That is really all there is to it. You can
download the template AddIn I used in this example here:
Customize Word2007 SC
Menu
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 |