What Is an Object

The dictionary definition of an object is "anything perceptible by one or more of the senses, especially something that can be seen and felt." Now, of course, you can't feel anything in an Office application, but you can see all kinds of things. To VBA, an object is anything in an application that you can see and manipulate in some way.

For example, a paragraph in a Word document is something you can see, and you can manipulate it by inserting text, changing the style, setting the font, and so on. A paragraph, therefore, is an object.

What isn't an object? The Office programs are so customizable that most things you can see qualify as objects, but not everything does. For example, the Maximize and Minimize buttons in document windows aren't objects. Yes, you can operate them, but you can't change them. Instead, the window itself is the object, and you manipulate it so that it's maximized or minimized.

You can manipulate objects in VBA in any of the following three ways:

■ You can make changes to the object's properties.

■ You can make the object perform a task by activating a method associated with the object.

■ You can define a procedure that runs whenever a particular event happens to the object.

To help you understand properties, methods, events, and objects, I'll put things in real-world terms. First, let's consider a simple analogy using a car. A car is an object, to be sure, but what does it mean to say that it has its own "properties, methods, and events"? Let's see:

■ The car's "properties" would be its physical characteristics: its model, color, engine size, and so on.

■ The car's "methods" define what you can do with the car: accelerate, brake, turn, and so on.

■ The car's "events" are the actions that happen to the car that generate an automatic response from the car. For example, on most modern cars, if you exit the vehicle with the key still in the ignition (the event), a warning bell will sound (the response).

Let's run through a more detailed example. Specifically, let's look at your computer as though it were an object. For starters, you can think of your computer in one of two ways: as a single object or as a collection of objects (such as the monitor, the keyboard, the system unit, and so on).

If you wanted to describe your computer as a whole, you would mention things such as the name of the manufacturer, the price, the color, and so on. Each of these items is a property of the computer. You also can use your computer to perform tasks, such as writing letters, crunching numbers, and playing games. These are the methods associated with your computer. There are also a number of things that happen to the computer that cause it to respond in predefined ways. For example, when you press the On button, the computer runs through its Power On Self-Test, initializes its components, and so on. The actions to which the computer responds automatically are its events.

The sum total of all these properties, methods, and events gives you an overall description of your computer.

But your computer is also a collection of objects, each with its own properties, methods, and events. The DVD drive, for example, has various properties, including its speed and data rate. Its methods would be actions such as inserting and ejecting a disc. A DVD drive event might be the insertion of a disc that contains a file that causes the disc's program to run automatically.

In the end, you have a complete description of the computer: what its distinguishing features are (its properties), how you interact with it (its methods), and to what actions it responds (its events).

0 0

Post a comment