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.3. Built-in Icons

A computer icon is a small graphic, usually ranging from 16 pixels by 16 pixels to upto 128 pixels by 128 pixels, which represents a file, folder, application or device on a computer system. Icons were first developed as a tool for making computer interfaces easier for novices to grasp in the 1970s at the Xerox Palo Alto Research Center facility. Icon-driven interfaces were later popularized by the Apple Macintosh and Microsoft Windows operating environments.

Icons may also be found on the toolbars and in the menus of programs such as Microsoft Word.

Virtually every major computer operating system now uses icon-based GUIs to display information to end users.

http://www.websters-online-dictionary.org/definition/icon

Resources

Template "AhWordBuiltinIcons.dot" 19 Kb, ZIP [download].
Template "AhWordBuiltinIcons2.dot" 24 Kb, ZIP [download].

Template "FaceID.dot" 15 Kb, ZIP [download] [download the original file].
Template "Icon Generator.dot" 38 Kb, ZIP [download] [download the original].
Template "BtnFaces.xla" 17 Kb, ZIP [download] [download the original].

Download all together!, 111 Kb, ZIP.

Functional Specification

Purpose

Create the template for convenient work with the built-in icons.

Solution

Built-in icons turned out to be very popular. Several solutions to the problem can be easily found in the i-net. First the template "AhWordBuiltinIcons.dot" was worked out on the basis of one of the solutions. Then a more beautiful solution was created, the template "AhWordBuiltinIcons2.dot". The sources and the descriptions of both solutions, as well as a brief review of the existing approach can be found in this etude.

Changing the existing solutions as you think best/ at your discretion/ at your pleasure is always a good intellectual exercise.

One always need serious reasons for inventing a bicycle.

FaceID.dot (Dave Rado, MVPs)

On the opening of the "FaceId.dot" template on the site MVPs http://www.mvps.org/ four windows with the built-in icons open. Opening your template along with "FaceId.dot" you can use any of the "FaceId.dot” built-in icons just copying them. When you point at any of the icons with the mouse cursor, the prompting message with the number of the FaceId icon appears. The largest number is 3518.

It is interesting to know that the template doesn’t contain any VBA code, only four toolbars with buttons.

 

Link: http://word.mvps.org/FAQs/Customization/CustomButtonImages.htm

 

Icon Generator.dot (Orlov A.I.)

On the opening of the "Icon Generator.dot" macro on A.I. Orlov’s site (http://orlovs.pp.ru/) three toolbars "Best Icons" and "Icon Generator" bar appear.

 

On pressing the "Icon Generator" button the following dialog appears:

After customization and pressing OK several new documents are created, each having the stated number of toolbars with built-in buttons. There is an example on the picture below:

You can add any of the built-in icons to your templates using the copy-paste method with the help of the "Customize" dialogue.

The Visual Basic code of the creation of the toolbars is available. The FaceId of the buttons is not.

Link: http://orlovs.pp.ru/soft.php

Functional Specification - continued

Resources

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

 

Toolbar "AhWordBuiltinIcons"

 

Button

Macro

Description

Help

AhBuiltInIconsHelp

Brief prompt on the toolbar buttons.

Create Toolbars

AhBuiltInIconsCreate

Creation of the toolbar with icons.

Delete Toolbars

AhBuiltInIconsDelete

Removal of the toolbar with icons.

 

Brief Prompt on Toolbar Buttons

Creating Toolbars with Built-in Icons

On pressing the "Create Toolbars" button the toolbars with the built-in icons are created.

Deleting Toolbars

On pressing the "Delete Toolbars" button the created toolbars with the built-in icons are deleted.

Implementation - Template "AhWordBuiltinIcons.dot"

'
' File AhWordBuiltinIcons.dot|AhWordBuiltinIcons
'
' Etudes for Microsoft Word Programmers.
' Etude 2.3. Built-in Icons (Solution #1).
'
' First published on http://www.transcriber.ru
'
' © 2000-2008. Evgeny Akhundzhanov. All rights reserved worldwide.
'

Option Explicit
'
' Ah! 29-Nov-2005. Builtin Word Icons
'
Const ui1000Index = 1
Const uiMaxIconsOnTheBar As Long = 50
Const strToolBarRootName As String = "AhBuiltinIcons"
'
' AhBuiltInIconsHelp
'
Sub AhBuiltInIconsHelp()
MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
   "Etude 2.3. Built-in Icons (Solution 1)." & vbCrLf & _
   "http://www.transcriber.ru" & vbCrLf & vbCrLf & _
   "Create - creates 20 toolbars, each contains 50 icons" & vbCrLf & _
   "Delete - deletes just created toolbars" & vbCrLf & vbCrLf & _
   "ui1000Index (hardcoded in the template) = [1-4] - Thousand index."

End Sub
'
' AhGetTBarName
'
Private Function AhGetTBarName(ByVal nBar As Long) As String
    AhGetTBarName = strToolBarRootName & CStr(nBar)
End Function
'
' AhBuiltInIconsCreate
'
Sub AhBuiltInIconsCreate()
Dim n As Long
Dim nBar As Long
Dim cbar As CommandBar
Dim butt As CommandBarControl
Dim strToolTip As String
Dim uiMinIconIndex As Long
Dim uiMaxIconIndex As Long
    uiMinIconIndex = (ui1000Index - 1) * 1000 + 1
    uiMaxIconIndex = (ui1000Index) * 1000
   
    nBar = 1
    Set cbar = CommandBars.Add(Name:=AhGetTBarName(nBar), Position:=msoBarTop, Temporary:=True)
    cbar.Visible = True
 
    For n = uiMinIconIndex To uiMaxIconIndex
        Set butt = cbar.Controls.Add(Type:=msoControlButton, ID:=797)
        With butt
            .FaceId = n
            strToolTip = "FaceId = " & CStr(n)
            .TooltipText = strToolTip
            If (n - uiMinIconIndex + 1) Mod uiMaxIconsOnTheBar = 0 And (n <> uiMaxIconIndex) Then
               
nBar = nBar + 1
                Debug.Print "nBar = " & nBar & " n = " & n & Chr(13)
               
                Set cbar = CommandBars.Add(Name:=AhGetTBarName(nBar), Position:=msoBarTop, Temporary:=True)
                cbar.Visible = True
            End If
        End With
    Next n
End Sub
'
' AhBuiltInIconsDelete
'
Sub AhBuiltInIconsDelete()
Dim nBar As Long
    On Error Resume Next
    For nBar = 1 To 40
        CommandBars(strToolBarRootName & CStr(nBar)).Delete
    Next nBar
End Sub

Second Solution based on Template "ButtonFaces.xla" by Jim Rech

After the creation of the first version of the "AhWordBuiltinIcons.dot" template, another, almost ideal solution was found. It is "ButtonFaces.xla" for Microsoft Excel by Jim Rech. The code was adapted to Microsoft Word and placed to the "AhWordBuiltinIcons2.dot" template.

Link: http://www.oaltd.co.uk/MVP/Default.htm

Resources

Template "AhWordBuiltinIcons2.dot", 24 Kb, ZIP [download].

Toolbar "AhWordBuiltinIcons2"

 

Button

Macro

Description

Help

AhBuiltInIcons2Help

Brief prompt on the toolbar buttons.

Show

AhBuiltInIcons2Show

Creation of the toolbar with icons.

Hide

AhBuiltInIcons2Hide

Removal of the toolbar with icons.

 

Brief Prompt on Toolbar Buttons

Creating Toolbar with Built-in Icons

Toolbar "Button Faces"

Button

Description

Tooltip

To the beginning.

The previous 100 icons. With the "Shift" pressed – the next 1000 icons.

 

The next 100 icons. With the "Shift" pressed – the previous 1000 icons.

To the end.

Hide the toolbar.

 

Implementation - Template "AhWordBuiltinIcons2.dot"

'
' File AhWordBuiltinIcons2.dot|AhWordBuiltinIcons2
'
' Etudes for Microsoft Word Programmers.
' Etude 2.3. Built-in Icons (Solution 2).
'
' First published on http://www.transcriber.ru
'
' © 2000-2008. Evgeny Akhundzhanov. All rights reserved worldwide.
'
' 17-Jan-2006. Jim Rech. "ButtonFaces.xla" project was adopted for MS Word.
' From The Excel MVP Page http://www.oaltd.co.uk/MVP/Default.htm
'
Option Explicit
 
Declare Function GetKeyState32 Lib "user32" Alias "GetKeyState" (ByVal vKey As Integer) As Integer
 
Const CtrlStNum As Integer = 108
 
Dim LastStartNumber As Long
Dim MaxStartNum As Long
Dim BtnFaceCB As CommandBar
Const strHelpEng As String = _
"This shows the button faces built into Microsoft Word." & vbCrLf & _
"You can use them in your own command bars " & vbCrLf & _
"(e.g., Commandbars(""X"").Controls(1).FaceID = 10). " & vbCrLf & _
"The number of FaceIDs available varies with the Word version, " & vbCrLf & _
"newer versions having more."
Const strHelpRus As String = _
"
Просмотр встроенных иконок Microsoft Word.  " & vbCrLf & _
"
Вы можете использовать их в своих панелях инструментов " & vbCrLf & _
"(
например, Commandbars(""X"").Controls(1).FaceID = 10).  " & vbCrLf & _
"Количество доступных иконок зависит от версии. " & vbCrLf & _
"В более новых версиях MS Word их обычно больше, чем в старых."
'
'
AhBuiltInIcons2Help
'
Sub AhBuiltInIcons2Help()
MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
  "Etude 2.3. Built-in Icons (Solution 2)." & vbCrLf & _
  "Jim Rech's 'ButtonFaces.xla' project was adopted for Microsoft Word." & _
  "http://www.transcriber.ru" & vbCrLf & vbCrLf & _
  strHelpEng
End Sub
'
' AhBuiltInIcons2Show
'
Sub AhBuiltInIcons2Show()
    ToolFaceCtrl
End Sub
'
' AhBuiltInIcons2Hide
'
Sub AhBuiltInIcons2Hide()
    KillCB
End Sub
'
' ToolFaceCtrl
'
Private Sub ToolFaceCtrl()
    KillCB
    MakeMainToolbar
    AdNavBtns
    AttachFaces 0
End Sub
'
' MakeMainToolbar
'
Private Sub MakeMainToolbar()
    Dim Ctrl As CommandBarControl
    Dim Counter As Integer
    Dim HoldWidth As Integer
    Set BtnFaceCB = CommandBars.Add("ButtonFaces", msoBarFloating, False, True)
    With BtnFaceCB
        .Protection = msoBarNoChangeVisible + msoBarNoResize + msoBarNoChangeDock
        .Top = GetSetting("JRRUtils", "BtnFacesTB", "Top", Application.Top + 50)
        .Left = GetSetting("JRRUtils", "BtnFacesTB", "Left", (Application.Left + Application.Width * 0.4))
        For Counter = 1 To 120
            With .Controls.Add(msoControlButton, 1)
                .FaceId = 1
            End With
            If Counter = 20 Then
                .Visible = True
                HoldWidth = .Width ''True width doesn't "happen" unless TB is visible
                .Visible = False
            End If
        Next
        MaxStartNum = MaxHundredStartNumber(.Controls(1))
        .Visible = True
        .Width = HoldWidth ''Width won't change unless TB is visible
        .Protection = msoBarNoChangeVisible + msoBarNoResize + msoBarNoChangeDock
        SetHelp
    End With
End Sub
'
' NextHundred
'
Private Sub NextHundred()
    If LastStartNumber < MaxStartNum Then
        If ShiftDown Then ''Go 1000 instead of 100
            AttachFaces MinOfTwo(MaxStartNum, LastStartNumber + 1000)
        Else
            AttachFaces LastStartNumber + 100
        End If
    End If
End Sub
'
' PriorHundred
'
Private Sub PriorHundred()
    If LastStartNumber >= 100 Then
        If ShiftDown Then
            AttachFaces MaxOfTwo(0, LastStartNumber - 1000)
        Else
            AttachFaces LastStartNumber - 100
        End If
    End If
End Sub
'
' LastHundred
'
Private Sub LastHundred()
    AttachFaces MaxStartNum
End Sub
'
' FirstHundred
'
Private Sub FirstHundred()
    AttachFaces 0
End Sub
'
' AttachFaces
'
Private Sub AttachFaces(StartNum As Long)
    Dim Counter As Integer
    On Error GoTo TooHigh
    LastStartNumber = StartNum
    SetNavBtnEnables
    With BtnFaceCB
        .Name = "Button Faces  (" & StartNum + 1 & " - " & StartNum + 100 & ")"
        For Counter = 1 To 100
            .Controls(Counter).FaceId = Counter + StartNum
            .Controls(Counter).TooltipText = Counter + StartNum
        Next
    End With
    Exit Sub
TooHigh:
    BtnFaceCB.Name = "Button Faces  (" & StartNum + 1 & " - " & StartNum + Counter - 1 & ")"
    ClearUnusedFaces Counter
End Sub
'
' ClearUnusedFaces
'
''When the last group is displayed this removes
'' the picture and tooltip from the unused buttons
Private Sub ClearUnusedFaces(FirstBtn As Integer)
    Dim Counter As Integer
    For Counter = FirstBtn To 100
        With BtnFaceCB.Controls(Counter)
            .FaceId = 1
            .TooltipText = ""
        End With
    Next
End Sub
'
' KillCB
'
Private Sub KillCB()
    On Error GoTo EndThis
    With BtnFaceCB
        SaveSetting "JRRUtils", "BtnFacesTB", "Top", .Top
        SaveSetting "JRRUtils", "BtnFacesTB", "Left", .Left
        .Delete
    End With
EndThis:
End Sub
'
' AdNavBtns
'
Private Sub AdNavBtns()
    With BtnFaceCB
         With .Controls(CtrlStNum)
            .FaceId = 154
            .OnAction = "FirstHundred"
            .TooltipText = "First group"
            .BeginGroup = True
        End With
        With .Controls(CtrlStNum + 1)
            .FaceId = 155
            .OnAction = "PriorHundred"
            .TooltipText = "Previous group" & vbCrLf & "Shift down to go back 1000"
         End With
         With .Controls(CtrlStNum + 2)
            .FaceId = 156
            .OnAction = "NextHundred"
            .TooltipText = "Next group" & vbCrLf & "Shift down to advance 1000"
        End With
        With .Controls(CtrlStNum + 3)
            .FaceId = 157
            .OnAction = "LastHundred"
            .TooltipText = "Last group"
        End With
        With .Controls(CtrlStNum + 4)
            .FaceId = 478
            .OnAction = "KillCB"
            .TooltipText = "Close"
         End With
        With .Controls(CtrlStNum + 5)
            .BeginGroup = True
         End With
   
    End With
End Sub
'
' SetNavBtnEnables
'
Private Sub SetNavBtnEnables()
    With BtnFaceCB
        .Controls(CtrlStNum).Enabled = (LastStartNumber <> 0)
        .Controls(CtrlStNum + 1).Enabled = (LastStartNumber <> 0)
        .Controls(CtrlStNum + 2).Enabled = (LastStartNumber <> MaxStartNum)
        .Controls(CtrlStNum + 3).Enabled = (LastStartNumber <> MaxStartNum)
    End With
End Sub
'
' MaxHundredStartNumber
'
''Returns to highest FaceID in the running version of Excel, rounded
'' down to the lower multiple of 100.  This determines the starting point
'' of the last button group.
Private Function MaxHundredStartNumber(Btn2Use As CommandBarButton) As Long
    Dim Counter As Long, Incr As Integer
    Dim BeginCounter As Integer
    On Error GoTo ErrHandler
    Incr = 1000
    BeginCounter = 1000
BeginSmallLoop:
    For Counter = BeginCounter To 10000000# Step Incr
        Btn2Use.FaceId = Counter
    Next
ErrHandler:
    If Incr = 1000 Then
        BeginCounter = Counter - 1000 + 100
        Incr = 100
        Resume BeginSmallLoop
    Else
        MaxHundredStartNumber = Counter - 100
    End If
End Function
'
' SetHelp
'
Private Sub SetHelp()
    With BtnFaceCB.Controls(101)
        .FaceId = 49
        .TooltipText = strHelpRus
    End With
End Sub
'
' MinOfTwo
'
Private Function MinOfTwo(Long1 As Long, Long2 As Long) As Long
    MinOfTwo = IIf(Long1 < Long2, Long1, Long2)
End Function
'
' MaxOfTwo
'
Private Function MaxOfTwo(Long1 As Long, Long2 As Long) As Long
    MaxOfTwo = IIf(Long1 < Long2, Long2, Long1)
End Function
'
' ShiftDown
'
Private Function ShiftDown() As Boolean
    ShiftDown = (GetKeyState32(16) < 0)
End Function

Conclusion

The work with the built-in icons is investigated in the etude. Any built-in icon can be used in visual as well as programming mode of the creation of toolbars.

Tasks

Task 1

In the given implementation the macros of the "AhWordBuiltinIcons.dot" template create 20 new toolbars, each with 50 buttons. It lets us see 1000 icons at the same time. To change the index of the 1000 some alterations of the VBA code are required.

Update the template so that the user could change the parameters of the procedure without accessing the VBA code, i.e. without the VBA editor, but changing the parameters in the user’s form. Make a project of the necessary form and implement it on your own.

 

Task 2

The repetitive pressing the "Create" button in the "AhWordBuiltinIcons.dot" template calls the macro AhBuiltInIconsCreate, which creates the next 20 toolbars, ignoring the fact that the toolbars with the same names already exist. Thus, Microsoft Word tolerates the existence of toolbars with the same names,

 

though the same action in the "visual" mode is prohibited.

Guess if it is a bug (Microsoft Word programmers’ mistake) or a feature (it was planned like this)?

Write the procedure with the following signature

 

Function ToolbarExists (strToolBarName As String) As Boolean

which returns True only in case the toolbar with the given name already exists.

Use the procedure to check the existence of the toolbar with the given name and avoid creating toolbars with the same names.

 

Task 3

Write a macro which saves all the icons of the selected toolbar in the specified folder on the hard disk. Examine the documentation if necessary.

 

 


Etudes for Microsoft Word Programmers. Etude 2.3. Built-in Icons


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.
www.transcriber.ru | E-mail the Author