Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Dynamic Toolbar

B e y o n d E x c el

PROJECTS

DYNAMIC TOOLBAR

2012 Craig Hatmaker

Dynamic Toolbar

2012 Craig Hatmaker

B e y o n d E x c el

Dynamic Toolbar

B e y o n d E x c el

Table of Contents
Why Read This? .................................................................................................................................................................................... 1
Which is best for me? ....................................................................................................................................................................... 1
Installing the Demo .............................................................................................................................................................................. 2
Dynamic Toolbar Introductory Video ............................................................................................................................................ 2
Requirements ................................................................................................................................................................................... 2
Installing ........................................................................................................................................................................................... 2
Running ............................................................................................................................................................................................ 2
Adding Dynamic Toolbar to Workbooks ............................................................................................................................................... 3
Configuring Dynamic Toolbar ............................................................................................................................................................... 4
TDT Toolbar Definition Table ..................................................................................................................................................... 5
Coding .................................................................................................................................................................................................. 7
Invoking Dynamic Toolbar with the Add-In ....................................................................................................................................... 7
Invoking Dynamic Toolbar without the Add-in .................................................................................................................................. 8
Coding Callbacks ........................................................................................................................................................................... 9
Examples: ..................................................................................................................................................................................... 9
Reading and Setting Control Values ................................................................................................................................................ 10
Creating Icons..................................................................................................................................................................................... 11
Technical Documentation ................................................................................................................................................................... 16
Introduction ................................................................................................................................................................................... 16
Components ................................................................................................................................................................................... 16
Forms ......................................................................................................................................................................................... 16
Modules ..................................................................................................................................................................................... 16
Classes (Event Handlers) ............................................................................................................................................................. 16

2013 Craig Hatmaker

Dynamic Toolbar

B e y o n d E x c el

Why Read This?

Beginners
Use this add-in to easily create
Ribbon-like interfaces for XL workbooks.
Developers
Learn about dynamically created
UserForms, Controls, Events, and Classes.
What is the Dynamic Toolbar?
When we need an easy and elegant
way for users to access macros/routines,
we have a few options:
1) Paste Buttons on Worksheets
From Developer tab find Controls
group and use Insert to paste a controls
to the worksheet, add text, set some
properties and assign a macro. Its easy
but takes up worksheet space and limits
our user interface to one worksheet or
we must replicate all controls to other
worksheets. It looks unprofessional.

2) The Office Custom UI Editor


This free standalone application
modifies any MS Office Ribbon. It uses
existing MS Office icons. It looks very
professional
but
requires
ribbon
configuration using XML in a separate
application. We cannot use it to modify
the Ribbon from within XL.
3) AJPs RibbonX Visual Designer
Andy Popes add-in is free and
provides a graphical editor. It is a
welcome step up from editing XML
directly.
4) Ribbon Commander
Spreadsheet1s add-in is free for trial
use.
It does everything The Office
Custom UI Editor does with VBA instead
of XML and from within XL so our
interface can change while our XL apps
are running.

5) Dynamic Toolbar
Dynamic
Toolbar
adds
a
customizable
floating
Ribbon-like
interface for XL applications like
PapaGantts and KitchenDesigns toolbars
shown above. Dynamic Toolbar is by far
the easiest to learn and use because we
configure it using normal Excel tables
instead of XML or VBA; and because it
requires only a handful of properties. It
also shares Ribbon Commanders ability
to change the toolbar while our app is
running.
Dynamic Toolbar cannot do all the
MS Office Ribbon does, but it does
enough; is instantly recognizable so users
know how to use it; and can be
assembled in a fraction of the time and
effort. It also happens to be a great way
to learn about adding, placing and
manipulating all kinds of controls on a
UserForm through VBA.

Which is best for me?


1.
2.

3.

I need quick and easy:


Dynamic Toolbar
My user interfaces are very sophisticated demanding a wide variety of controls
or
I must preserve as much screen real estate as possible:
I prefer a graphical tool:
AJPs RibbonX Visual Designer
I prefer to work in XML:
The Office Custom UI Editor
I prefer to work in VBA:
Ribbon Commander
I need tab layouts to change while my application is running
And point #2 applies:
Ribbon Commander
Point #2 does not apply:
Dynamic Toolbar or Ribbon Commander

2014 Craig Hatmaker

Page 1

Dynamic Toolbar

B e y o n d E x c el

Installing the Demo

Dynamic Toolbar
Dynamic Toolbar Introductory Video
http://youtu.be/hNuiTlLmGWE

Requirements

Installing

Running

This demo requires MS Excel 2007


or later. VBA (macros) must be enabled.

IMPORTANT! Follow these steps.


Dont skip any.

1.
2.
3.

Open the extracted folder


Double click AddInDemo.xls
If Protected View displays click
Enable Editing.

4.

If Security Warning displays click


Enable Content.

1.
2.
3.
4.
5.

Download DynamicToolbar.Zip
Open your Downloads folder, NOT
the zip file.
Right click DynamicToolBar.Zip and
select Extract All.
Make sure Show extracted files when
complete is checked.
Make note of the extracted folders
path.

Select a row or rows in any table and


use SHIFT-CTRL-D to display the Toolbar
defined by the AddIns Toolbar Definition
Table (TDT).

DynamicToolbar.Zips URL: https://dl.dropboxusercontent.com/u/13737137/Projects/DynamicToolbar/DynamicToolbar.zip

2014 Craig Hatmaker

Page 2

Dynamic Toolbar

B e y o n d E x c el

Adding Dynamic Toolbar to Workbooks


Play the video first to get an overview of Dynamic Toolbar. Then play with AddInDemo.xls to get familiar with it. Next, read
Configuring Dynamic Toolbar to see how simple it is to customize for your project. Lastly, add it to your projects by following these
simple steps:

1.

Copy TDT from AddInDemo.xls to your project (or create it).


Reference: TDT Toolbar Definition Table

2.

Modify the TDT entries to define your toolbars controls


Reference: Configuring Dynamic Toolbar

3.

Create or copy icons into your projects directory (if using Button or Command control types).
Reference: Creating Icons

4.

Add the Dynamic Toolbar add-in, OR copy the three Dynamic Toolbar modules to your project and code to display the
Dynamic Toolbar when your workbook opens and/or when the user presses CTRL-T
Reference: Invoking Dynamic Toolbar with the Add-In
Or
Invoking Dynamic Toolbar without the Add-in

5.

Record or write macros/routines for each control to run and name them exactly as your entered in TDTs Call Back column

6.

Make sure the routines have the appropriate parameters for the control type.
Reference: Coding Callbacks

When these six steps are completed, Dynamic Toolbar is ready for your project.

2014 Craig Hatmaker

Page 3

B e y o n d E x c el

Dynamic Toolbar

Configuring Dynamic Toolbar


Tab

Button

TextBox

DropBox

CheckBox

Group

Configuring
Dynamic
Toolbar
involves making entries in an XL table
that I call the TDT (Toolbar Definition
Table). Below is the TDT used to create
the above Dynamic Toolbar.

We can name this table anything we


want but it must contain the seven
columns shown. Each table row defines a
control. The first row defines the Project
Name TextBox and includes the Tab and

Group names, the Control type, the


TextBoxs Caption and initial Setting.
Thats it! Just a handful of entries
add a textbox to a toolbar.

TDT
Toolbar Definition Table
Tab
Group
PapaGantt Identification
PapaGantt Identification
PapaGantt Database
PapaGantt Database
PapaGantt Database

- This defines what controls are included in the Dynamic Toolbar


Control Caption
Call Back
Values
TextBox Project Name
TextBox Task ID Prefix
Button
Import Project
modGantt.ImpPrj Icons\Import48.gif
Button
Export Project
modGantt.ExpPrj Icons\Export48.gif
Button
Remove Project
modGantt.DrpPrj Icons\Remove48.gif

PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt
PapaGantt

Button
Button
Button
Button
Button
Button
Button
DropDown
DropDown
CheckBox
CheckBox
CheckBox
Button
Button
Button

Actions
Actions
Actions
Actions
Actions
Actions
Actions
Settings
Settings
Options
Options
Options
About
About
About

2014 Craig Hatmaker

Clear Task Table


Increase Task Indent
Decrease Task Indent
Create WBS
Recalc Durations
Draw/ Redraw Gantt
Print Gantt
Level Display
Interval
Dependency Lines
Cursor Control
Show Input Messages
About
Guide
Beyond Excel

modGantt.ClrTbl
modGantt.Indent
modGantt.Outdent
modGantt.CrtWBS
modGantt.ClcTsk
modGantt.CrtCht
modGantt.PrtCht
modGantt.ChgLvl
modGantt.ChgInt
modGantt.DspLin
modGantt.CsrCtl
modGantt.DspIM
modGantt.DspAbt
modGantt.DspPDF
modGantt.DspBXL

Icons\Clear48.gif
Icons\Indent48.gif
Icons\Outdent48.gif
Icons\WBS48.gif
Icons\Recalc48.gif
Icons\Draw48.gif
Icons\Print48.gif
*All,0,1,2,3,4,5,6,7,8,9
Years,Quarters,Months,Weeks,Days,Hours,30,15,5,1

Setting
Project
1

*All
Weeks
TRUE
TRUE
TRUE

Icons\About48.gif
Icons\Acrobat48.gif
Icons\BXL48.gif

Page 4

B e y o n d E x c el

Dynamic Toolbar
TDT Toolbar Definition Table

The TDT has only seven columns; which means it takes no more, and sometimes fewer than seven entries to define a Toolbar
control. Here is what each column is:
Column/Field
Tab

Description
This determines which Groups and Controls display in Dynamic Toolbar. Unlike MS
Offices Ribbon, Dynamic Toolbar currently displays only one tab at a time, but we
can define multiple tabs in the same TDT and change which tab to display in
Dynamic Toolbar at any time.
ATTENTION ENTERPRISING DEVELOPERS!
Try adding a MultiPage control to Dynamic ToolBar to enable multiple tabs.

Group

This determines which Controls appear in a Group. Grouping controls can add order
to Dynamic Toolbar and improve its appearance. Group captions appears at their
bottom.

Control

Dynamic Toolbar only recognizes the most common control types:


Button

This is a large icon with its caption underneath. Use


buttons to run macros/routines requiring no
parameters.

Command

This is a small icon with its caption to the


right. Use command buttons when small icons are good enough
or desired for aesthetics.

DropBox

This is a dropdown list and caption to the left. Use


DropBoxes to select from a list of options.
List values are entered in the Values field.

CheckBox

This displays a check box with its caption to


the right. Use CheckBoxes to turn features on and off.

TextBox

Use text boxes for entering names, captions, or other free form
text or properties.

ATTENTION ENTERPRISING DEVELOPERS!


Try adding all Ribbon controls to Dynamic ToolBar.
Caption

All controls have captions. Enter one, two, or three words to explain what the
control does.

Call Back

This is a Controls assigned macro/routine.


NOTE! If a routine comes from a workbook other than the host workbook, enter
the routines fully qualified name. Example:
MyWorkbook.xls!MyModule.MyRoutine

2014 Craig Hatmaker

Page 5

Dynamic Toolbar
Column/Field
Values

Description
This column/field is dependent on what is in the control column/field. For:

Setting

2014 Craig Hatmaker

B e y o n d E x c el

Buttons and Commands this is the image file (with path if not in the same
directory as the workbook).
ComboBoxes this contains values to list.
CheckBoxes and TextBoxes this does not apply

ComboBoxes, CheckBoxes and TextBoxes get their initial values from here. They
also set this value when they change. Our routines can get the Dynamic Toolbar
controls current values from here.

Page 6

Dynamic Toolbar

B e y o n d E x c el

Coding
Invoking Dynamic Toolbar with the Add-In
The purpose of Dynamic Toolbar is to run macros/VBA routines and there is a little VBA required to set up the add-in.
NOTE!

Running AddInDemo.xlsm installed the Dynamic Toolbar add-in on your PC. With Dynamic Toolbar installed we
can load it into our projects manually using the DEVELOPER tab, Add-Ins button, checking Dynamictoolbar in the
Add-Ins dialog and then clicking OK

or we can use VBA.


Private Sub Workbook_Open()
Me.Activate
Application.AddIns("DynamicToolbar").Installed = True
Application.EnableEvents = True
Application.OnKey "^T", "ThisWorkbook.Toolbar"
Toolbar
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^T", ""
Application.AddIns("DynamicToolbar").Installed = False
End Sub
Public Function Toolbar() As Boolean
Me.Activate: Run "'DynamicToolbar.xlam'!Display", [TDT].ListObject, "PapaGantt"
End Function

Add the above code to your projects ThisWorkbook module to load the
DynamicToolbar add-in when our workbook opens. The code also contains one
way to launch the toolbar using the keyboard shortcut Shift-Ctrl-T (We can
choose a different key if we want to preserve the Table Totals keyboard
shortcut).
NOTE! Change the Tab. The above code launches tab PapaGantt. Change PapaGantt to whatever your tabs name is.
NOTE! Run "'DynamicToolbar.xlam'!Display" is an example of calling an external VBA routine. Even though
add-ins are loaded with a host workbook, add-ins are not part of the host workbook. They are external to the
host workbook. To call external routines we must use Application.Run or, more concisely, the Run method.
NOTE! Tables - VBA knows XLs tables as ListObjects. When we create a table in XL it receives a name that can be
found in XLs Name Manager. I use TDT as the Toolbar Definition Tables name. We can use XLs Names in
VBA using VBAs Evaluate method. Brackets around Names are shorthand for VBAs Evaluate method. Thus,
[TDT] is shorthand for Evaluate(TDT). VBAs Range object also evaluates names like this: Range(TDT)
so [TDT] is also shorthand for Range(TDT); thus, [TDT] evaluates to the tables Range, not its ListObject.
If a Range is part of a table, we can get the ListObject from the Range like this: Range(TDT).ListObject,
or more concisely [TDT].ListObject
2014 Craig Hatmaker

Page 7

Dynamic Toolbar

B e y o n d E x c el

Invoking Dynamic Toolbar without the Add-in


The add-in is DynamicToolbar.xlam and appears in our VBEs Project window. It
contains three modules that do all the work: frmDynTB, modGeneral, and clsDynTB.
frmDynTb

Is a completely blank UserForm with code for adding controls.

modGeneral

Is a stripped down version of my general purpose library containing


routines used in other projects that I reuse here.

clsDynTB

Is a class module that handles various control events. This is required


because frmDynTb has no controls until it adds them, thus, it has no
code for handling any control events. clsDynTb does. We can create
as many instances of clsDynTB as needed, one for each control added
to frmDynTB, and clsDynTB will handle all click, change, and mouse
over events.

We can drag these three modules from DynamicToolbar.xlam into our project and
not bother with using an add-in. This may make project distribution easier and coding
Dynamic Toolbar invoking routines simpler.

Private Sub Workbook_Open()


Application.OnKey "^T", "ThisWorkbook.Toolbar"
Toolbar
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^T", ""
End Sub
Public Function Toolbar() As Boolean
frmDynTB.Display [TDT].ListObject, "PapaGantt"
End Function

2014 Craig Hatmaker

Page 8

B e y o n d E x c el

Dynamic Toolbar
Coding Callbacks

A callback is just another term used in the Microsoft ribbon community for a macro (or routine) assigned to a ribbon control.
Dynamic Toolbar is not a ribbon. It just looks and acts like a ribbon. I am leveraging ribbon terms to prepare readers who go on to
customizing the ribbon. While callbacks are simply macros/routines assigned to a ribbon control, different ribbon control types
require different callback stubs (macro/routine templates). The table below provides examples.

Control
Buttons,
Commands

Description
Have no required parameters.

ComboBoxes

Require one variant parameter that will contain


whatever users select from the list.

Public Sub MyList_Change(byVal vValue as Variant)

Require one boolean parameter that will be TRUE


when the control is checked and FALSE when
unchecked.

Public Sub MyCheckBox_Click(byVal bOnOff as Boolean)

Require one string parameter that will contain


whatever is in the textbox.

Public Sub MyTextBox_Change(byVal sString as String)

CheckBoxes

TextBoxes

Callback Stub Example


Public Sub MyButton_Click()
End Sub

End Sub

End Sub

End Sub

NOTE!

All callbacks can contain optional parameters, and all parameters, including the first, can be declared Optional.
Dynamic Toolbar will only use the required parameter listed in the table above.

NOTE!

Controls are not required to have a callback at all. Setting an option, for example, might not need an immediate
reaction and but its setting could be required by some operation not yet started.

Examples:
Scenario
Button or Command
Display worksheet Sales. The Buttons callback is DisplayChart.
CheckBox
Turn EnableEvents on or off. The CheckBoxs callback is SetEvents.
ComboBox
Set font size. The ComboBoxs callback is FontSize.

2014 Craig Hatmaker

Code
Public Sub DisplayChart()
Worksheets(Sales).Activate
End Sub
Public Sub SetEvents(byVal bOnOff as Boolean)
Application.EnableEvents = bOnOff
End Sub

Public Sub FontSize(byVal vValue as Variant)


Selection.Font.Size = clng(vValue)
End Sub

Page 9

B e y o n d E x c el

Dynamic Toolbar
Reading and Setting Control Values

If a routine needs to know which ComboBox option is selected, or if a CheckBox is checked, we can provide our routines the
current setting from the TDTs Setting column. We can also examine frmDynTBs controls directly. Getting values from frmDynTBs
controls requires three pieces of information:

Question
Is frmDynTB displayed?
Which tab Is frmDynTB displaying?
Which control is Project Name textbox?

Code Solution
If frmDynTB.Visible then
IF frmDynTB.Caption = sMyTabName then
Dim lRow
Dim oMyControl

as Long
as Object

lRow = Evaluate("MATCH(""Project Name"", TDT[Caption],0)")


Set oMyControl = frmDynTB.Controls(Format(lRow,"000"))

NOTE!

frmDynTBs control names are their TDT row formatted to three positions with leading zeroes. Ex: 001

2014 Craig Hatmaker

Page 10

Dynamic Toolbar

B e y o n d E x c el

Creating Icons
Creating icons is easy. The easiest way is to simply copy an image and reduce it to the size we need. Here is an example.
Imagine we just recorded a report macro and need a Report icon so users can click it on the Dynamic Toolbar and create the report
from freshly downloaded data. Where can we get a Report icon?
Instructions
Open browser and navigate to Google

Screen Shots

Type the name of what you need


followed by the word Icon and press
Enter. In this example, we want an icon
for an option to create a Report.

2014 Craig Hatmaker

Page 11

Dynamic Toolbar
Instructions
Chances are this screen will not provide
what we need so click Images.

B e y o n d E x c el

Screen Shots

Scroll down until you find an image that


looks good to you. Keep in mind that
large fancy images will not look good
when shrunk to small icons. Look for
small, simple images. In this example, I
picked a simple blue icon.

2014 Craig Hatmaker

Page 12

Dynamic Toolbar
Instructions
Right click on the image and select Copy
Image.

B e y o n d E x c el

Screen Shots

Open Microsoft Paint and click Paste


then click Resize.

2014 Craig Hatmaker

Page 13

Dynamic Toolbar
Instructions
Make sure Pixels is clicked and then set
Horizontal to 50 pixels. If Maintain
aspect ratio is checked (the default) we
do not need to set the Vertical to 50.

B e y o n d E x c el

Screen Shots

NOTE! For Command icons resize the


image to 16 x 16 pixels.

When Horizontal and Vertical pixel


values are set, click OK.

Click Crop

2014 Craig Hatmaker

Page 14

Dynamic Toolbar
Instructions
Click the file dropdown and then select
Save As and one of these picture
formats: JPEG, BMP, or GIF (I prefer
JPEG).

B e y o n d E x c el

Screen Shots

Save the icon and make note of its path


and name.

Congratulations! You have just made an


icon that can be included in Dynamic
Toolbar.

2014 Craig Hatmaker

Page 15

B e y o n d E x c el

Dynamic Toolbar
Technical Documentation
Introduction
The two main components
interest are frmDynTB and clsDynTB.

of

clsDynTB responds to all events


related to the toolbar controls.

frmDynTB provides a blank form to


which buttons, textboxes and combo
boxes are added as needed.

The routines in modGeneral are


routines common to other BXL projects
(ex. Color and DspErrMsg) or are

routines provided as examples (Display


and SetControl)

Components
Forms
Form
frmDynTB
This UserForm starts out with no controls.
Routine AddControls adds all controls.

Routines
AddControls

Description
Creates all controls for TDT rows matching a Tab name.

Display

Displays the toolbar

Fill

Fills combo boxes with the valid values as designated by


the TDTs value column.

FindGridCell

Determines where to place a control on the toolbar.

Routine
DspErrMsg

Description
This is a stripped down version of my normal DspErrMsg.
For more information about how I prefer to handle errors
see: Beyond Excel: Error Handling.

Color

Provide the appropriate color.

Display

Sample routine for displaying toolbar

SetControl

Sample routine for setting a toolbar controls value.

Method/Property
<Control>_MouseMove

Description
See Glow

<Control>_Click

See Change

Modules
Module
modGeneral
General routines

Classes (Event Handlers)


Class
clsDynTB
Provides data validation input messages
to frmData

<Control>_Change

2014 Craig Hatmaker

Setup

Associates control types to designated WithEvents


variables

Change

Responds to clicking on a control or changing its value.

Page 16

Dynamic Toolbar
Class

2014 Craig Hatmaker

B e y o n d E x c el

Method/Property
QualifyCallBack

Description
Makes sure routines are called from the appropriate
project.

Glow

Turn on Glow effects

ResetColor

Turn off Glow effects

Page 17

You might also like