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.1. Built-in Toolbars

Menu
1. A list of dishes available at a restaurant

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

In this etude we will study some properties of Microsoft Word toolbars and their controls. Also we investigate the "visual" process of creating a toolbar (with the help of Microsoft Word built-in tools, without using VBA), adding buttons and menus to the toolbar, editing button properties and skills of the built-in icon editor.

Resources

Template "AhToolBarsBuiltIn.dot", 22 Kb, ZIP [download].

Functional Specification

Task

The task is to create three macros for printing Microsoft Word built-in toolbar properties. The first macro AhToolBarsBuiltinPrint1  should print toolbar names together with toolbar indices. The second macro AhToolBarsBuiltinPrint2 should request the toolbar name from the user and print names of all toolbar controls together with their indices and icon numbers (a.k.a. faceIDs). The third macro AhToolBarsBuiltinPrint3 should do the same as AhToolBarsBuiltinPrint2 does but for all toolbars. The verb "print" here imply the text output formatted as table, so we also will learn how to create simple tables in the Microsoft Word document.

The code and the "AhToolBarsBuiltIn" toolbar should be placed to the template "AhToolBarsBuiltIn.dot". The  "AhToolBarsBuiltIn" toolbar should contain buttons "Help", "Command Bars", "Command Bar Controls" and "All Command Bar Controls".

Toolbar "AhToolBarsBuiltin"

The template document "AhToolBarsBuiltin" looks like this:

Brief description of the "AhToolBarsBuiltin" toolbar buttons is given in the table below.

Button

Macro

Description

Help

AhToolBarsBuiltinHelp

Brief prompt on the toolbar buttons.

CommandBars

AhToolBarsBuiltinPrint1

All Microsoft Word toolbars.

Command Bar Controls

AhToolBarsBuiltinPrint2

Properties of the selected toolbar controls.

All Command Bar Controls

AhToolBarsBuiltinPrint3

Properties of all toolbars controls.

Brief Prompt

Brief prompt looks like this:

Command Bars

Macro AhToolBarsBuiltinPrint1 ("CommandBars" button) should produce output like this:

Command Bars

Index

Name

1

Standard

2

Formatting

3

Tables and Borders

4

Database

5

Drawing

6

Forms

7

Full Screen

8

Edit Picture

9

Visual Basic

10

Stop Recording

11

Mail Merge

12

Master Document

13

Microsoft

14

Header and Footer

15

Outlining

16

Print Preview

17

Word for Windows 2.0

18

Read Mail

19

Send Mail

20

Extended Formatting

21

AutoText

22

Web

23

WordArt

24

3-D Settings

25

Shadow Settings

26

Picture

27

Reviewing

28

AutoSummarize

29

Exit Design Mode

30

Control Toolbox

31

Text Box

32

Outlook Read Mail

33

Outlook Send Mail

34

Function Key Display

35

Web Tools

36

Menu Bar

37

Refresh

38

Frames

39

AhToolBarsBuiltIn

40

Drop Caps

41

Endnotes

42

Fields

43

Display Fields

44

Field Display List Numbers

45

Form Fields

46

Footnotes

47

Frames

48

Headings

49

Linked Headings

50

Script Anchor Popup

51

Lists

52

Inline Picture

53

Horizontal Line Popup

54

Tables

55

Table Cells

56

Table Headings

57

Table Lists

58

Table Pictures

59

Table Text

60

Whole Table

61

Linked Table

62

Text

63

Linked Text

64

Font Popup

65

Font Paragraph

66

Spelling

67

Grammar

68

Grammar (2)

69

Background Proofing Status Bar

70

Track Changes

71

Frame Properties

72

Hyperlink Context Menu

73

Hyperlink Menu

74

AutoSignature Popup

75

Field AutoText

76

Document Map

77

Shapes

78

Curve

79

Curve Node

80

Curve Segment

81

Floating Picture

82

OLE Object

83

ActiveX Control

84

WordArt Context Menu

85

Rotate Mode

86

Comment

87

Track Changes Indicator

88

Chinese Translation

89

Inline ActiveX Control

90

AutoShapes

91

Callouts

92

Flowchart

93

Block Arrows

94

Stars & Banners

95

Lines

96

Basic Shapes

97

Fill Color

98

Line Color

99

Align or Distribute

100

Rotate or Flip

101

Order

102

Nudge

103

Borders

104

Font Color

105

Shading Color

106

Cell Alignment

107

Text Wrapping

108

Clipboard

Command Bar Controls

At first the macro asks the user to type the toolbar name

If there is no toolbar with such name then an error message is displayed.

Macro AhToolBarsBuiltinPrint2 ("CommandBar Controls" button) should produce output like this:

Toolbar 'Standard' Controls

 

Index

Caption

FaceId

2520

New &Blank Document

3813

23

&Open...

23

3

&Save

3

3738

&Mail Recipient

3738

2521

&Print

2521

109

Print Pre&view

109

2566

&Spelling and Grammar...

2566

21

Cu&t

21

19

&Copy

19

22

&Paste

22

108

&Format Painter

108

128

Can't &Undo

 

129

Can't &Redo

 

1576

Hyperl&ink...

1576

916

&Tables and Borders Toolbar

916

333

&Insert Table...

 

142

&Insert Excel Spreadsheet

 

9

&Columns...

 

204

&Drawing

204

1714

&Document Map

1714

119

&Show All

119

1733

&Zoom:

 

984

Microsoft Word &Help

984

All Command Bars Controls

The macro AhToolBarsBuiltinPrint3 ("All CommandBar Controls" button) produces the same output as AhToolBarsBuiltinPrint2, but for all toolbars. The full output takes more than 40 pages of A4 format and is omitted.

Implementation - template AhToolBarsBuiltin.dot

Module AhToolBarsBuiltin

'
' File AhToolBarsBuiltin.dot|AhToolBarsBuiltin
'
' Etudes for Microsoft Word Programmers.
' Etude 2.1. Built-in Toolbars.
'
' First published on http://www.transcriber.ru
'
' 2000-2008. Evgeny Akhundzhanov. All rights reserved worldwide.
'
Option Explicit

Procedure AhToolBarsBuiltinHelp

'
' AhToolBarsBuiltinHelp
'
Sub AhToolBarsBuiltinHelp()
    MsgBox "Etudes for Microsoft Word Programmers." & vbCrLf & _
        "Etude 2.1. Built-in Toolbars." & vbCrLf & _
        "http://www.transcriber.ru" & vbCrLf & vbCrLf & _
        "Command Bars - Lists all Toolbars" & vbCrLf & _
        "Command Bar Controls - Lists Controls of the Selected Toolbar" & vbCrLf & _
        "All Command Bar Controls - Lists all Toolbars and Controls"
End Sub

Procedure AhArrayPrintAsTable

The procedure AhArrayPrintAsTable inserts the header and a new table at the end of the active document and then fills the table with array arrName elements.

'
' AhArrayPrintAsTable
'
Sub AhArrayPrintAsTable(ByVal tblIndex As Long, ByVal strHeading As String, ByVal dwHeadFontSize As Long, arrName() As String, ByVal nCols As Long, ByVal dwTextFontSize As Long)
    Dim nCol, nLine, nLines As Long
    nLines = UBound(arrName) / nCols
   
    Selection.EndKey Unit:=wdStory
 
    Selection.InsertParagraph
    Selection.InsertBefore Text:=strHeading
    Selection.Font.Size = dwHeadFontSize
    Selection.Collapse direction:=wdCollapseEnd
   
    Selection.InsertParagraph
    Selection.Font.Size = dwTextFontSize
    Selection.Collapse direction:=wdCollapseEnd
 
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=nLines, NumColumns:=nCols
 
    With ActiveDocument.Tables(tblIndex)
        ' .Rows(1).HeadingFormat = True
        .Borders.OutsideLineStyle = wdLineStyleThinThickLargeGap
        .Rows.AllowBreakAcrossPages = False
        .AutoFitBehavior (wdAutoFitContent)
    End With
 
    nLine = 0
    For nCol = 1 To nCols
        With ActiveDocument.Tables(tblIndex).Cell(nLine + 1, nCol).Range
            .InsertAfter arrName(nCol - 1)
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .Font.Size = dwTextFontSize
            .Font.Bold = True
        End With

    Next nCol
 
 
    For nLine = 1 To nLines - 1
 
    For nCol = 1 To nCols
        With ActiveDocument.Tables(tblIndex).Cell(nLine + 1, nCol).Range
            .InsertAfter arrName(nCols * nLine + nCol - 1)
            .Font.Size = dwTextFontSize
        End With
    Next nCol
   
    Next nLine
 
    Selection.HomeKey Unit:=wdStory
   
End Sub

Procedure AhToolBarDump

'
' AhToolBarDump
'
Sub AhToolBarDump(ByVal tblIndex As Long, ByVal strCBarName As String, ByRef CBControls As CommandBarControls)
Dim ctrl As CommandBarControl
Dim item As CommandBarControl
Dim nItems, nItemsTotal As Long
   
    nItemsTotal = 0
   
    For Each ctrl In CBControls
        nItems = 0
        On Error Resume Next
        nItems = ctrl.Controls.Count
        nItemsTotal = nItemsTotal + 1 + nItems
        Debug.Print ctrl.Caption, ctrl.ID
        If nItems > 0 Then
            For Each item In ctrl.Controls
                Debug.Print "   ", item.Caption, item.ID
            Next item
        End If
    Next ctrl
   
    Const nColumns As Long = 3
    Dim arrName() As String
    ReDim arrName(nColumns * (nItemsTotal + 1))
    arrName(0) = "Index"
    arrName(1) = "Caption"
    arrName(2) = "FaceId"
    Dim k As Long
    k = nColumns
   
    For Each ctrl In CBControls
        nItems = 0
        On Error Resume Next
        nItems = ctrl.Controls.Count
        arrName(k) = ctrl.ID
        arrName(k + 1) = ctrl.Caption
        arrName(k + 2) = ctrl.FaceId
        k = k + nColumns
        If nItems > 0 Then
            For Each item In ctrl.Controls
                arrName(k) = item.ID
                arrName(k + 1) = item.Caption
                arrName(k + 2) = item.FaceId
                k = k + nColumns
            Next item
        End If
    Next ctrl
   
    Dim strTitle As String
    strTitle = "Toolbar '" & strCBarName & "' Controls"
   
    AhArrayPrintAsTable tblIndex, strTitle, 16, arrName, nColumns, 10
   
End Sub

Procedure AhToolBarsBuiltinPrint1

'
' AhToolBarsBuiltinPrint1
'
Sub AhToolBarsBuiltinPrint1()
Dim cbar As CommandBar
    '
    ' Prints names for all Command Bars
    ' There are appr. 107 of them for MS Word 2000
    '
    Const nCols As Long = 2
    Dim arrName() As String
    ReDim arrName(nCols * (CommandBars.Count + 1))
    arrName(0) = "Index"
    arrName(1) = "Name"
   
    Dim k As Long
    k = nCols

    For Each cbar In CommandBars
        Debug.Print cbar.Index & "," & cbar.Name
        arrName(k) = cbar.Index
        arrName(k + 1) = cbar.Name
        k = k + nCols
    Next cbar
   
    Documents.Add
    'Application.WindowState = wdWindowStateNormal
 
    AhArrayPrintAsTable 1, "Command Bars", 16, arrName, nCols, 10
       
End Sub

Procedure AhToolBarsBuiltinPrint2

'
' AhToolBarsBuiltinPrint2
'
Sub AhToolBarsBuiltinPrint2()
    Dim strTBName As String
   
   
strTBName = InputBox("Enter toolbar name: ")
   
    Documents.Add
   
    On Error GoTo ExitLabel
    With CommandBars(strTBName)
        AhToolBarDump 1, .Name, .Controls
    End With
    Exit Sub
   
ExitLabel:
    ActiveDocument.Close
    MsgBox "Invalid toolbar name"
End Sub

Procedure AhToolBarsBuiltinPrint3

'
' AhToolBarsBuiltinPrint3
'
Sub AhToolBarsBuiltinPrint3()
Dim cbar As CommandBar
   
    Documents.Add
   
    Dim tblIndex As Long
    tblIndex = 1
   
    For Each cbar In CommandBars
        AhToolBarDump tblIndex, cbar.Name, cbar.Controls
        tblIndex = tblIndex + 1
    Next cbar
 
End Sub

Creating Custom Toolbar in "Visual" Mode

Here we will investingate simple "visual" toolbar creation and editing. "Visual" imply using Microsoft Word customization capabilities without using VBA.

Creating New Toolbar

Select menu item "Tools\Customize". In the "Customize" dialog select tab "Toolbars" and press the "New" button.

In the "New Toolbar" dialog enter the toolbar name, select the template to store the new toolbar in, and press the "OK" button. The above actions result in a new empty toolbar with name "Custom 1".

Press the "Close" button to close the "Customize" dialog.

Adding Buttons and Menus

In the "Customize" dialog select "Commands" tab and then select "Macros" item in the "Categories" list. The "Commands" list on the right side will display the list of all available commands (i.e. public VBA procedures that have no arguments) for all open documents and templates.

Just drag the required commands from the "Commands" list and drop them to the new toolbar. This results in the new buttons that correspond to the selected commands. To delete a button from the toolbar just drag it from the toolbar and drop it to the "Customize" dialog.

The result looks like this:

To create a menu on the toolbar select "New Menu" in the "Categories" list, drag it and drop to the toolbar. Adding menu items is just the same as adding new buttons - just drop the required commands to the toolbar's menu.

Editing Button Properties

To edit button properties enter "Customize" mode (provided that the "Customize" dialog is visible) and right click the required button to access the context menu.

Editing Options

All available editing commands are described in the following table. Basically, all commands are responsible for changing the button representation (text, icon, text and icon).

Command

Description

Reset

Resets toolbar control to its initial state.

Delete

Delete control.

Name

Control caption. For new commands contains the full name of the macro.

Copy Button Image

Copies button image to the clipboard.

Paste Button Image

Pastes button image from the clipboard (provided that somebody copied the required image to the clipboard before).
Pasting sutomatically scales the image.

Reset Button Image

Resets button image to its initial state.

Edit Button Image

Editing button image using the built-in icon editor.

Change Button Image

Here the user can select one of the 42 built-in images.

Default Style

Displays only icon.

Text Only (Always)

Displays only text (always).

Text Only (in Menus)

Displays only text (in menus).

Image and Text

Displays icon and text.

Begin a Group

Inserts group separator on the left side of the control.

Assign Hyperlink

Assigns a hyperlink to the button. Instead of running a macro such button will follow an assigned hyperlink. After a hyperlink is assigned, a "Delete Hyperlink" command will appear, which allows the user to delete a hyperlink assignment.

Built-in Icon Editor

The built-in icon editor window looks like this:

Conclusions

The "visual" customization mode is intended for the creation of simple toolbars, which contain only buttons and menus (msoControlButton and msoControlPopup controls). For the majority of users it is enough. For more complex toolbars you need VBA.

The toolbar creation using VBA is investigated in the next etude 2.2 "Cutom Toolbars".

Tasks

Task 1

The standard Microsoft Word configuration contains too many commands (toolbars and buttons). Create your own toolbar "My Commands" and place all commands (from all Microsoft Word toolbars) that you really use to it. Make all toolbars that you don't use or use from time to time invisible. Try to live one week using only one "My Commands" toolbar. If necessary add commands that you miss. In a week estimate the convenience of the proposed method.

 

Task 2

Create your own custom template with toolbar or use one of the global templates from the previous etudes. Run the macros and output properties of the custom toolbar. Do custom toolbar properties differ from built-in toolbar properties? If yes, what is the difference?

 

Task 3

The macros above just print some toolbar and control properties. Learn help information about toolbars and their controls. Are there more properties that it is interesting to print? Change macros so that they print the required toolbars and controls properties.

 

 


Etudes for Microsoft Word Programmers. Etude 2.1.


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