Use a button when you need to save space on a toolbar. However, make
sure that you add a name to the button that explains what the button is
used for, or else you might forget its purpose. Use a menu item to make
the button’s purpose easier to remember. Choosing the menu item
option displays a new toolbar button. The new button has the name that
you assigned to the button displayed on the toolbar,
making it easier to
see what the button does.
3. To add a custom button, drag a Custom Menu Item object from the
Customize dialog box to the Standard toolbar.
The application should add a new (blank) button to the toolbar.
4. Right-click the new button on the toolbar, and then select the Name
option. (This entry has a text box next to it.)
5. In the Name field, type
&Say Hello
.
This action assigns a name to the button. The ampersand (&)
places an
underline beneath the
S
in
Say.
A user can press Alt+S to access the
button quickly when the toolbar is selected.
6. Right-click the new button on the toolbar, and then select Assign Macro.
An Assign Macro dialog box appears, like the one shown in Figure 2-9.
7. Highlight the
SayHello
entry, and then click OK.
8. Click Close to close the Customize dialog box.
That’s it! The Say Hello toolbar button is now functional. Click it to see the
same two dialog box sequences that you see in the previous examples.
Figure 2-9:
Use the
Assign
Macro
dialog box
to add a
macro to the
toolbar
button.
44
Part I: An Overview of VBA
06_046500 ch02.qxp 12/5/06 5:33 PM Page 44
Defining a menu entry
You might use a program often enough to attach it to a regular program ele-
ment but not often enough to take up space on a toolbar. In this case, you can
use a menu to hold the quick-launch option when working with versions of
Office that don’t rely on the Ribbon interface. You
use the same set of steps
shown in the preceding “Defining a toolbar button” section to perform this
task. The only difference is that you drag the custom button or custom menu
item to the menu that you want to use instead of to a toolbar (Step 3).
Accessing the program
from other VBA code
Never write a piece of code twice when you can write it once and use it every-
where. Saving
time is one reason to use VBA, so saving time by writing VBA
code is a good way to increase the benefits that you receive. In Chapter 3, I
discuss many of the methods that you can use to reuse code. However, the
first idea that you have to understand is that you can
call
(tell VBA to execute)
any VBA program that you create from another program. Here’s a simple
example that you can add to the Code window:
Sub SayHello2()
‘ Show that we’re using the SayHello2 program.
MsgBox “We’re in SayHello2!”
‘ Call SayHello
SayHello
End Sub
Notice how this sample uses pseudo-code to describe what happens when
you run the program.
Remember that
pseudo-code
is a list of steps, written in
a form that you can understand, that the program must perform. The first
task is to prove that you executed the
SayHello2
program
by displaying a
message box containing a message that doesn’t appear in
SayHello
. The
second task is to call
SayHello
. Make sure that you save your program at
this point.
When you run this program, it displays three message boxes. The first one
reads
We’re in SayHello2!
The second
and third message boxes look
just like the ones you see for the
SayHello
program used throughout this
chapter.
45
Do'stlaringiz bilan baham: