2. Introducing Visual Basic for Applications

In Previous Blog, you learned some of the important pre-settings that needs to be done before you get started with PowerPoint VBA. This Blog explains what VBA is, describes how VBA fits into the zone of object-oriented computer languages, and gets you familiar with some basic terms and symbols that you will use when you start writing VBA code in the next Blog.

  • What Is Visual Basic for Applications?

Visual Basic for Applications (VBA) is a very powerful object-oriented programming language that can be used to add to the functionality of Microsoft Office applications, including PowerPoint. Originally, PowerPoint was a presentation tool, used by many to enhance lectures, sometimes making them better and sometimes making them worse. While PowerPoint can still be used as a presentation tool, it becomes more powerful as an interactive tool.

While PowerPoint’s interactivity is very powerful and useful, it is also very limited. VBA extends this to nearly unlimited possibilities. With VBA, you can change the content and appearance of slides based on student input, you can ask for and process typed user inputs, you can add additional slides, you can hide and show graphics, and much more. You will learn the basics of scripting in VBA beginning in upcoming Blogs. First, we’ll try to learn a little bit about what object oriented programming is.

  • What Is an Object-Oriented Programming Language?

First of all, VBA is a programming language. Don’t let this to bother you . . .too much.! VBA isn’t just an ordinary programming language; it is an object-oriented programming (OOP) language. An OOP language has three key features: classes, objects, and methods. Read it again so that its marks a place in your mind.

Classes are types of things, objects are specific things, and methods are what you do to things. For example, there is a class of 5 books called “PowerPoint books”, and each book covers dedicated PowerPoint topics like Basics, Design, Animations, Transitions and VBA. The specific phone book on my study table (Let’s say, it’s a VBA book) is an object. I can do many things with a PowerPoint book, such as look up a specific topic to read, turn to a page, mark few important points etc., All the things that I could do to this VBA book are methods.

Let’s consider below piece of code,

Dim vbaBook As PowerPointBook
vbaBook.LookUpTopic "Object-Oriented Programming Language"

The first line says that vbaBook is a specific instance (an object) of the class PowerPointBook. This tells us that all the things we can do with PowerPoint books in general can be done to this specific VBA book. Since one of the things that we can do with PowerPoint books is look up a specific topic to read, we do that on the second line. vbaBook.LookUpTopic says that for this specific PowerPoint book, call the method (do the action) LookUpTopic . Since we need to know which Topic to look up, this method takes a parameter (information that the method needs to complete its job). That information comes after the method, sometimes in parentheses. Since the information is text, we put it in quotes.

All the details are important. The dot (that period between vbaBook and LookUpTopic) is necessary to tell the computer that LookUpTopic is the thing to do (method) with the object vbaBook. Putting the information right after the method with a space in between tells the computer that the stuff after the method is important information (parameters) for knowing what the method should do. The quotes tell the computer that what’s inside them is text. IF YOU MISS OUR ANY OF THESE MINOR DETAILS, NOTHING WILL WORK!

Another critical point about objects is that they can have parts. Think about our VBA book example. It can have Main cover Pages and also many normal pages between these cover pages. Each of these parts is its own object. You might access the VBA book by accessing a part of the book. For example, consider below code,

vbaBook.Pages.TurnToTheNextPage

This code will turn the page to the next one, so if you are on page 15, for example, you will find yourself on page 17 (if the page is two sided). Now the dot is serving two purposes. The first dot says that Pages is a part of the object vbaBook , and the second dot tells the computer to do the thing (run the method) TurnToTheNextPage , which is something that can be done to Pages.

While some parts of an object are other objects, some parts are properties. For example, a VBA book has a color, several pages, and a thickness etc. So, for example, if I wanted to see how thick my VBA book is, I might look at that property:

vbaBook.thickness

Now, with this basic understanding of objects, classes, methods and properties, you will be able to understand the basics of OOP when these terms show up. Now are you thinking about how it relates to PowerPoint??!! PowerPoint has many objects and classes. A typical PowerPoint presentation contains many slides. That’s a class. As a class, Slide is the specific type of thing that you find in PowerPoint and Slides is the collection of all the individual slides in a presentation. The set of slides in your specific presentation is an object. That set of slides contains individual slides. A slide might contain many elements or shapes. Think about a slide with a text box, a shape, and an image. Perhaps these are shapes 1, 2, and 3 on the slide. They each have many properties, such as whether or not they are visible. Because a text box, a shape, and an image are all members of the class Shape and shapes may be visible or not, we can look at the Visible property of these objects.

For example:

ActivePresentation.Slides(2).Shapes(3).Visible

Above code looks at the current PowerPoint presentation (ActivePresentation). That presentation contains slides (ActivePresentation.Slides). We want to look at the second slide (that’s the 2 in parentheses), and we want to look at the third shape on that slide ( Shapes(3) ). Finally, that shape, like all shapes, can be visible or not, so we want to look at the Visible property.

If you don’t understand the details of object-oriented programming languages, then don’t worry. You will be able to pick it up as you read through next blogs.

Leave a Reply