• No results found

First of all, VBA is a pro gramming language. Don’t let this scare you . . . too much. Hav ing a background in computer science and pro gramming would be helpful, and you will not be able to take full ad vantage of VBA with out be - coming (at least) a nov ice pro grammer. How ever, this book guides you through some of the basic things you might want to do with VBA with out the need of any pro gram ming background.

To top it off, VBA isn’t just an or dinary pro gramming language; it is an ob - ject-oriented pro gramming (OOP) lan guage. An OOP lan guage has three key features: classes, objects, and methods. Classes are types of things, objects are specific things, and methods are what you do to things. For example, there is a class of things called “phone books.” The spe cific phone book on my desk is an object. I can do many things with a phone book, such as look up a person’s phone number, turn to a page, put it on a chair for my four-year-old daughter to sit on, etc. All the things that I could do to the phone book are methods. If we convert this phone book ex ample to computerese (computerese is not a real computer language, but it plays one on TV), we might have the following:

Dim myPhoneBook As PhoneBook

myPhoneBook.LookUpPerson("John Smith")

The first line says that myPhoneBookis a spe cific instance (an ob ject) of the class PhoneBook. This tells us that all the things we can do with phone books in general can be done to this specific phone book. Since one of the things that we can do with phone books is look up a specific person, we do that on the sec ond line.myPhoneBook.LookUpPersonsays that for this spe cific phone book, call the method (do the action)LookUpPerson. Since we need to know which per son to look up, this method takes an ar gument (in formation that the method needs to complete its job). That in formation is put in pa rentheses af ter the method. Since the in formation is text, we put it in quotes, too.

Computers are very picky. All the de tails are important. The dot (that pe- riod be tweenmyPhoneBookandLookUpPerson) is nec essary to tell the com - puter that LookUpPerson is the thing to do (method) with the ob ject

myPhoneBook. The parentheses tell the computer that the stuff in side is impor- tant information (parameters) for knowing what the method should do. The What Is an Ob ject-Oriented Pro gramming Language? 35

quotes tell the computer that what’s in side them is text. Leave out any de tail, and nothing will work.

Another critical point about ob jects is that they can have parts. Think about our phone book example. Think about what parts there are to a phone book. Here are a few ex amples: the cover, pages, the blue pages (for gov ernment listings), and the phone company in formation (such as how to contact the phone com pany if your phone stops work ing). Each of these parts is its own ob ject (a particular page might be an ex ample of the classPage, or a range of pages might be an ex - ample of the classPages). You might access the phone book by accessing a part of the book. For example,

myPhoneBook.Pages.TurnToTheNextPage

might take the set of pages and turn them to the next one, so if you are on page 57, for example, you will find your self on page 59 (if the page is two-sided). Now the dot is serving two purposes. The first dot says thatPagesis a part of the ob jectmyPhoneBook, and the second dot tells the com puter to do the thing (run the method) TurnToTheNextPage, which is something that can be done to

Pages.

While some parts of an ob ject are other objects, some parts are properties. For ex ample, a phone book has a color, a number of pages, and a thickness. So for ex ample, if I wanted to see how thick my phone book is, I might look at that prop erty:

myPhoneBook.thick ness

or I might want to add two thicknesses together to get something tall enough for my daugh ter to sit on and be able to reach the table:

myPhoneBook.thickness + myNeighborsPhoneBook.thick ness

Finally, we turn to in heritance, and then you won’t be an ex pert in OOP, but you will be able to play one on TV. We have been look ing at the class

PhoneBook. Well, is n’t a phone book just a spe cific type of book? Therefore, we could think of aPhoneBook as a type of Book that in herits all the prop erties and methods from books. The ob ject I am work ing with is stillmyPhoneBook, but it is not only a member of the classPhoneBook, it is (since PhoneBook is a sub class ofBook) also a mem ber of the classBook. Ev erything you can do with a book, in general, you can do with a phone book . . . and more. For example, you can turn pages in a book, look at the cover, weigh down pa pers, etc. You can also look up a phone num ber or find in formation about area codes in a phone book, but not in all books.

Now, with this ba sic un derstanding of ob jects, classes, and methods, you will be able to un derstand the basics of OOP when these terms come up.

Before leaving OOP, think about how it re lates to PowerPoint. PowerPoint has many ob jects and classes. A typical PowerPoint presentation con tains many 36 In tro duc ing Vi sual Ba sic for Ap pli ca tions

slides. Slides! That’s a class. As a class,Slides is the col lection of all the in di- vidual slides in a presentation. The set of slides in your specific presentation is an ob ject. That set of slides con tains in dividual slides. A slide might con tain many ob jects or shapes. Think about a slide with a text box, a piece of clip art, and a button. Perhaps these are shapes 1, 2, and 3 on the slide. They each have many properties, such as whether or not they are vis ible. Because a text box, a piece of clip art, and a button are all mem bers of the classShape and shapes may be visible or not, we can look at the Vis i ble property of these objects. For example:

ActivePresentation.Slides(3).Shapes(2).Vis i ble

This looks at the current PowerPoint presentationActivePresentation. That pre sen ta tion con tains slidesActivePresentation.Slides. We want to look at the third slide (that’s the 3 in pa rentheses), and we want to look at the second shape on that slide (Shapes(2)). Finally, that shape, like all shapes, can be vis i- ble or not, so we want to look at the Vis i ble prop erty. So, what that small piece of code says is: Look at theVis i ble prop erty of the second shape, which is one of the shapes, on the third slide, which is one of the slides, in the current PowerPoint presentation. It’s a good thing we can use VBA be cause we would get pretty tired typ ing out long sentences like that.

If you don’t un derstand the details of ob ject-oriented pro gramming languages, don’t worry. Because you are learning to be a scripter, you will be able to pick it up as you go along. The more you un derstand, the easier it will be to change scripts to suit your pur poses, but to start, you only need to type the scripts you see.