Chapter 5. Working with Objects

     

In this chapter

  • Understanding what objects are and why they're important

  • Learning to work with object properties, methods , and events

  • Assigning an object to a variable

  • An easy way to work with objects that have multiple properties or methods

  • Taking a close look at your first two objects: the Application object and the Window object

Many of your VBA procedures will perform calculations using simple combinations of numbers , operators, and the host application's built-in functions. You'll probably find, however, that most of your code manipulates the application environment in some way, whether it's formatting document text, entering data in a worksheet range, or setting application options. Each of these items ”the document, the range, and the application ”is called an object in VBA. Objects are perhaps the most crucial concept in VBA programming, and I'll explain them in detail in this chapter.

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 like 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 the On button is pressed, 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 CD-ROM drive, for example, has various properties, including its speed and data rate. Its methods would be actions such as ejecting a disk and adjusting the sound level. A CD-ROM 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).



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net