Word Document Properties


Chapter 2: Your First VBA Program



Download 8,82 Mb.
Pdf ko'rish
bet49/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   45   46   47   48   49   50   51   52   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Chapter 2: Your First VBA Program
06_046500 ch02.qxp 12/5/06 5:33 PM Page 43


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

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   45   46   47   48   49   50   51   52   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish