Chapter 1: Understanding VBA 7
What are the main elements used in VBA?
If you’ve ever developed object-oriented code in C++, Borland Delphi, or Java, you’re already familiar with the
concepts of “classes,” “objects,” “properties,” and “methods,” but let’s re-examine them in greater detail as they
apply to VBA.
A class is a description of something. For example, the class “car” is a small vehicle with an engine and four
wheels.
An object is an instance of a class. If we extend the car metaphor, then the actual, physical car that you go out and
purchase for the purposes of driving is an object (that is, an instance of the class “car”). In the context of
CorelDRAW, each open document is an instance of the
Document class, each page in the document is an
instance of the
Page class, and each layer (and each shape on each layer) are more instances of more classes.
Most classes have properties. For example, the properties of the class “car” are that it is small, it has an engine, and
it has four wheels. Every instance of the class “car” (that is, every object in that class) also has properties such as
color, speed, and number of seats. Some properties, called “read-only” properties, are fixed by the design of the
class; for example, the number of wheels or seats does not (usually) vary from car to car. However, other
properties can be changed after the object has been created; for example, the speed of the car can go up and
down, and, with a bit of help, its color can be changed. In the context of CorelDRAW,
Document objects have a
name, a resolution, and horizontal and vertical ruler units; individual shapes have outline and fill properties, as
well as a position and a rotation factor; and text objects have text properties, which may include the text itself.
A method is an operation that the object can have performed on itself. In the example of the class “car,” the car can
be made to go faster and slower, so two methods for the class are “accelerate” and “decelerate.” In the context of
CorelDRAW, documents have methods for creating new pages, layers have methods for creating new shapes,
and shapes have methods for applying transformations and effects.
Objects are often made up of other smaller objects. For example, a car contains four objects of the class “wheel,”
two objects of the class “headlight,” and so on. Each of these child objects has the same properties and methods
of its class-type. In the context of CorelDRAW, a document object contains page objects, which contain layer
objects, which contain shape objects, some of which contain other objects. This parent/child relationship of
objects is an important one to recognize, particularly when referencing an individual object.
Some classes “inherit” features from their parents. For example, in the context of CorelDRAW, the
Shape type
has many subtypes (or “inherited types”), including
Rectangle, Ellipse, Curve, and Text. All of these
subtypes can make use of the basic members of the
Shape type, including methods for moving and transforming
the shape and for setting its color. However, the subtypes also have their own specialist members; for example, a
Rectangle can have corner radii, whereas Text has an associated Font property.
What is an object model?
VBA relies on an application’s object model for communicating with that application and modifying its documents.
Without an object model, VBA cannot query or change an application’s documents.
Object models in software provide a high level of structure to the relationship between parent and child objects.
They also allow object types or classes to be used repeatedly in different ways; for example, a
Shape object may
be of the type “group” and may contain other
Shape objects, some of which may also be of the type “group,” or