Microsoft Outlook 2003 - Article 01 Programming Forms with VBScript.pdf

(1621 KB) Pobierz
CDA1615144.fm
Article 1
Programming Forms with
VBScript
Understanding Scripting . . . . . . . . . . . . . A2
A Brief Overview of VBScript. . . . . . . . . . A2
Understanding Objects and
Properties . . . . . . . . . . . . . . . . . . . . . . . . A2
Understanding Events and Methods . . . . A4
Creating Scripts and Using the
Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . A4
Referencing Controls. . . . . . . . . . . . . . . . A6
Using Methods . . . . . . . . . . . . . . . . . . . . A7
Event Handling . . . . . . . . . . . . . . . . . . . . A9
Using Variables. . . . . . . . . . . . . . . . . . . . A9
Using Constants . . . . . . . . . . . . . . . . . . A11
Retrieving and Setting Field
Values. . . . . . . . . . . . . . . . . . . . . . . . . . A11
Retrieving User-Defined and Other
Properties. . . . . . . . . . . . . . . . . . . . . . . A12
For More Information on VBScript
and Outlook . . . . . . . . . . . . . . . . . . . . . A16
You can often develop a reasonably powerful custom solution in Microsoft Office Outlook
2003 simply by creating and using Outlook forms with standard and custom controls. How-
ever, this approach has limits. For example, what if you want a new task to be created every
time a user sends your custom form? What if you want to send a meeting request when your
form is submitted? What if you want to connect to a database and fill in a drop-down list on
your form with values from a specific database table?
Although it isn’t practical to explain how to code for all these problems in this introductory
chapter, the preceding list offers good examples of some of the tasks that are difficult, if not
impossible, to accomplish using only custom forms that have no underlying custom program
code. This chapter looks at developing custom form solutions with computer code and cus-
tom fields. You’ll strengthen your understanding of programming basics and learn how you
can begin to apply this understanding to developing a custom Outlook solution.
To create a solution that involves working with dynamic data, launching new items, and ani-
mating other programs, you could search for a control that already has these behaviors built
in. However, don’t spend too much time looking, because you’re unlikely to find a control
with functionality so specifically tailored to your needs. The way to create powerful forms is
by writing code.
For some people, writing code in an Outlook solution simply means learning the syntax of
Microsoft Visual Basic, Scripting Edition (VBScript) and some of the properties, methods,
and events of Outlook and Outlook forms. For other people, writing code might seem much
more daunting. No matter what your level of programming experience is, this chapter pro-
vides some basic knowledge you need to begin programming Outlook forms.
A1
731482453.003.png
Microsoft Office Outlook 2003 Inside Out
Understanding Scripting
Programming languages such as C++, Microsoft Visual Basic, VBScript, and C# (pro-
nounced “C sharp”) provide access to computer resources at different levels. The basic rule is
that the “lower” you go, the more you need to know. Think of it as similar to programming
a microwave: when you enter a combination of settings to defrost a chicken that weighs 1.3
pounds, you’re programming the oven to do certain things. At a lower level, a program inside
the microwave takes the settings you provide and translates them into a cooking plan that the
machine can execute. Most often, lower level programming languages, which are usually
designed for speed and scalability, are used to create sophisticated, complex applications.
However, the learning curve for these languages can be prohibitive for someone who wants to
simply use some controls to create a form that sends an e-mail message. This is where a
higher level language such as VBScript steps in, giving the power of programming to users
who don’t have extensive backgrounds in computer science.
As the name implies, VBScript is a scripting language . A scripting language can create an
unlimited number of programmatic solutions relatively quickly. Always remember, however,
that you are trading true power and performance for simplicity and speed of development.
Unlike a compiled programming language, a scripted language is interpreted—that is, the
statements of instruction are reduced to a simple sequence of statements that are then exe-
cuted by what is called a command interpreter . For each scripting language, a corresponding
scripting engine provides this interpreter, along with other basic services to execute the
script. When you program Outlook forms, you don’t need to do anything to install this
engine or ensure that your scripts will actually be executed. Outlook has this engine built in;
all you need to do is open the Script Editor and begin writing code.
Because Outlook uses VBScript as the language for form development, you need to know the
syntax of VBScript. You also need to know which interfaces are available for you to manipu-
late with lines of code. The next section provides this information.
A Brief Overview of VBScript
VBScript is a subset of the Microsoft Visual Basic programming language. The VBScript
engine is portable, which means the engine can be embedded in or used by many different
programs, such as Microsoft Internet Explorer or Outlook. As mentioned earlier, VBScript is
already embedded in Outlook. You can begin writing form-based code, and those who use
your forms will benefit from the code behind the forms without installing anything special to
make the forms work. Because the VBScript engine is a true subset of the complete Visual
Basic language, it is less complex and therefore easier to learn.
Understanding Objects and Properties
Object is probably one of the most overused words in the world of computer programming.
Over time, the word has come to represent many different things to people in the IT field.
Because the focus of this book is not hard-core computer science, this chapter is relatively
introductory and covers only what you need to get your Outlook solution moving along. In
A2
731482453.004.png
Programming Forms with VBScript
this context, the following section discusses the notion of an object and what it means to your
Outlook solution.
Objects
At the simplest level, an object in your Outlook development environment represents Out-
look, the program itself, and other elements in Outlook (such as a text box) that expose prop-
erties, methods, or events with which you can interact programmatically. Outside the world
of computing, the definition of an object is widely understood. For example, you know that
a book is an object. It has certain properties such as height, weight, color, number of pages,
title, author, and so on. You can also perform certain actions with a book: you can open it,
close it, turn pages one at a time, flip to the end, return to the beginning, and fold the corners
to mark your location.
A significant aspect of programming a custom solution using VBScript or Microsoft Visual
Basic for Applications (VBA) is manipulating Outlook items. For example, you add a text
box to a form and then add or change the text it displays by setting a property of that text box
object. You display or close a form by using a Show or Hide method on the form, treating the
form as a complete object rather than showing or hiding the individual controls on the form.
When a user clicks a button on a form, that button object generates a Click event. You add the
code you want Outlook to execute when the button is clicked.
Thinking of Outlook items as programmable objects will help you begin to understand how
you can create custom solutions with VBScript and VBA. Don’t think of your custom pro-
gram as pages and pages of program code. Instead, think of it as a collection of forms, con-
trols, and other objects with properties you can set, either at design time or through program
code when the form runs, to make it perform the tasks you intend.
Properties
Properties are attributes of an object, describing some aspect of how an object behaves. To
return to an earlier analogy, an object in Outlook can have some of the same properties as a
book. For example, a form has height, width, and other basic properties that describe appear-
ance. A command button has properties such as height, width, position, caption, name, and
color. The more you know about the properties of the objects you use, the more you can do
with those objects. To set a property of an object, you must reference the object and follow it
with a period and then the name of the property. The following code sample shows how to set
the property of an object:
cmdAddAppointment.Caption = "Add Appt."
In this example, assume that your form contains a command button named cmdAddAppoint-
ment . You manipulate the Caption property of that button by setting it to some new value.
How do you know that the button has this property? For any given object, you must read
documentation to know the available properties and other behaviors for that object. For
example, a text box does not have a Caption property, but a label does; both these controls
have a Font property.
A3
731482453.005.png
Microsoft Office Outlook 2003 Inside Out
Notice that in the preceding example the Caption property required a string of characters
between quotes. This is because the Caption property is predefined to accept characters
strung together and not numbers, bitmaps, or other kinds of data. When you work with
properties, you need to learn which types of data each property accepts to accurately apply
property settings for your objects.
Understanding Events and Methods
Events and methods are two other powerful behaviors of a form or other object in Outlook. To
continue with the previous analogy, you can do a number of things with a book: you can
open it, close it, and so on. Put another way, the book possesses a number of methods, such
as OpenBook , CloseBook , Tu r n Pa g e , GoToEnd , GoToBeginning , and AddBookMark . These
method names are intuitive. The creators of any object model usually give intuitive names to
the methods that their object exposes.
You can also associate certain events with a book. For example, when you finish reading the
book, you might have to return it to the library. When you open the book for the first time,
you might have to remove the book’s dust jacket. To return to the microwave analogy, when
the microwave completes the cooking plan you’ve provided, this event causes an alarm to
sound. In other words, the microwave has a CookingPlanCompleted event that fires when the
time runs out. When this event occurs, the alarm sounds.
In the world of Outlook development, you’ll interact with many objects, some of which
expose certain events. Arguably, the most common event to fire is some kind of Click event,
which fires when a user clicks a command button or other control. Notice the terminology:
an event fires when something else happens. This is important to remember, because it is easy
to get methods and events confused. Just remember that methods are actions you can take
with an object, whereas events fire in response to an action—perhaps in response to a
method or the changing of a property setting.
Creating Scripts and Using the Editor
Now that you have a little background information about the properties, methods, and
events of the objects you can include in your solution, you’re ready to begin writing code.
Where do you write it? Theoretically, you can write the code in any program you want—in
Microsoft Word, Notepad, or any other text editor. However, where you write the code is not
as important as where it must reside to be able to run. Outlook has a window, or code editor,
where all code must be placed before it can run. To access this code editor in Outlook, choose
Form, View Code when your form is open. You can also click the View Code button to the
right of the Control Toolbox button. In either case, the window shown in Figure A1-1
appears. Any code that you want to run for your Outlook form must be contained in this
window.
A4
731482453.006.png
Programming Forms with VBScript
FA1ok01
Figure A1-1. You can write and run the code for your form in the Script Editor window.
Inside Out
Outlook Object Library Help
The Outlook Script Editor provides roughly the same level of functionality as Notepad or
another basic text editor. However, the menu bar of this editor does contain the Outlook
Object Library Help, shown in Figure A1-2. The Object Library will come in handy as you
develop your programming skills in Outlook. To access this help, choose Help, Microsoft
Outlook Object Library Help from the editor’s command bar. It’s helpful to take some time
to understand the Outlook object model.
FA1ok02
Figure A1-2. Use Outlook Object Library Help when writing code for Outlook.
A5
731482453.001.png 731482453.002.png
Zgłoś jeśli naruszono regulamin