Etudes for Microsoft Word Programmers

Home ] Up ] Etude 2.1. Built-in Toolbars ] Etude 2.2. Custom Toolbars ] Etude 2.3. Built-in Icons ] Etude 2.4. Icon Instruments ] Etude 2.5. Modifying Toolbars ]

Etude 2.5. Modifying Toolbars

                                                                    A Workaround is a bypass of a recognized problem in a system. A workaround is typically a temporary fix that implies that a geniune solution to the problem is needed.
Frequently workarounds are as creative as true solutions, involving out-of-the-box thinking in their creation.


Template "", 19 Kb, ZIP [download].

Functional Specification


Very often you have to perform actions that modify the toolbar itself. The actions are as follows:

  • changing the accessibility/visibility of a button or a toolbar - properties Enabled, Visible

  • changing of the text of the button - Caption property

  • changing of the tool tip - TooltipText property

  • changing of the control icon (see two previous etudes)

If the toolbar is located in the "" template and the template file doesn’t have the attribute "read only", then the solution is easy. If the toolbar is in the source template, located in the autorun folder, then each time you close Microsoft Word the unpleasant request appears, inquiring if the user is going to save the changes or not.

Using automatic macros won’t help because the request appears before AutoClose or AutoExit macros begin to work.

The only way out is a bypass (called workaround), in fact, it is cheating Microsoft Word. It consists in setting the value True for the property Saved of the current template.

The drawback of this solution is the necessity of using the filename in the template code – if you rename the template file you have to change the template code as well.


The "" template contains a number of macros and the "AhToolBar01" toolbar, meant for testing the modified toolbar. The "AhToolbar01" toolbar looks like this:






Brief prompt on the toolbar buttons.

Test AhToolBarModifyTest Testing button. Its properties/characteristics are changed by the following three buttons.



Changes the Caption property.



Changes the Toolbar property.
Enabled AhToolBarModifyEnabled Changes the Enabled property.

Brief Prompt

The brief prompt looks like this:

Implementation - Template ""

' File|AhToolBarModify
' Etudes for Microsoft Word Programmers.
' Etude 2.5. Modifying Toolbars.
' First published on
' © 2000-2008. Evgeny Akhundzhanov. All rights reserved worldwide.
Option Explicit
Private bEnabled As Boolean
Private nPress As Long
Private strBtnName As String



' Constants
Private Const cStrThisToolBarName As String = "AhToolBarModify"
Private Const cStrThisTemplateName As String = ""
Private Const ciBtnTestIndex As Long = 2
Private Const cStrButtonName As String = "Test"

Subroutine AhToolBarModifyHelp

' AhToolBarModifyHelp
Sub AhToolBarModifyHelp()
    MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
        "Etude 2.5. Modifying Toolbars." & vbCrLf & _
        "" & vbCrLf & vbCrLf & _
        "The following commands change properties of the 'Test' button" & vbCrLf & _
        "Name - changes .Caption property" & vbCrLf & _
        "Tooltip - changes .Tooltip property" & vbCrLf & _
        "Enabled - toggles .Enabled property."
End Sub

Subroutine AhToolBarModifyTest


' AhToolBarModifyTest
Sub AhToolBarModifyTest()
MsgBox "'AhToolBarModifyTest' pressed."
End Sub

Subroutine AutoExec


' AutoExec - initialization.
Sub AutoExec()
bEnabled = False
    nPress = 0
    strBtnName = cStrButtonName
End Sub

Subroutine SaveItSelf


' SaveItSelf - makes template with name cStrThisTemplateName "Saved".
Private Sub SaveItSelf()
    Dim tt As Template
    ' Templates.Item(cStrThisTemplateName) - "The requested item of collection doesn't exists."
    ' find template with requested name
    For Each tt In Templates
        ' Debug.Print tt.Name & vbCrLf
        ' Use case-insensitive compare for file names.
        If LCase(tt.Name) = LCase(cStrThisTemplateName) Then
            tt.Saved = True
            ' tt.Save
            ' Debug.Print cStrThisTemplateName & " saved."
End If
    Next tt
End Sub

Subroutine AhToolBarModifyName

' AhToolBarModifyName
Sub AhToolBarModifyName()
    nPress = nPress + 1
    strBtnName = CommandBars(cStrThisToolBarName).Controls(ciBtnTestIndex).Caption
    With CommandBars(cStrThisToolBarName).Controls(strBtnName)
        .Caption = cStrButtonName + CStr(nPress)
    End With
' SaveItSelf
End Sub

Subroutine AhToolBarModifyTooltip

' AhToolBarModifyTooltip
Sub AhToolBarModifyTooltip()
    nPress = nPress + 1
    strBtnName = CommandBars(cStrThisToolBarName).Controls(ciBtnTestIndex).Caption
    With CommandBars(cStrThisToolBarName).Controls(strBtnName)
        .TooltipText = cStrButtonName + CStr(nPress)
    End With
' SaveItSelf
End Sub

Subroutine AhToolBarModifyEnabled

' AhToolBarModifyEnabled
Sub AhToolBarModifyEnabled()
    bEnabled = Not bEnabled
    strBtnName = CommandBars(cStrThisToolBarName).Controls(ciBtnTestIndex).Caption
    With CommandBars(cStrThisToolBarName).Controls(strBtnName)
        .Enabled = bEnabled
    End With
    ' SaveItSelf
End Sub



The work with the modifying toolbar is investigated in the etude. A workaround is implemented that allows avoiding Microsoft Word message box that suggests saving changes to the template.

The "" template was created on the basis of specification. It contains macros for working with the modified toolbar and prevents "Do you want to save the changes" message box to appear.


Task 1

Experiment with the template. Does it always work? Are all factors considered in the work of the SaveItSelf procedure? Make the procedure better if necessary.


Task 2

You made some improvements to the template, but your code changes were not saved. Why? What has to be done to force the changes to be saved?


Etudes for Microsoft Word Programmers. Etude 2.5. Modifying Toolbars.

Unless otherwise noted, all materials on this site are
© 2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site. | E-mail the Author