PowerPoint Creative
Log In or Register to participate in PPC!

Share
View previous topicGo downView next topic
avatar
rusnakcreative
Administrator
Administrator
Posts : 241
Join date : 2017-08-16
https://www.rusnakcreative.com

How to VBA - Lesson 2: I Object!

on Thu Oct 12, 2017 9:49 pm
Message reputation : 100% (1 vote)
(The I stands for Intro to "Objects", but who's counting anyways?)


Recap
To recap, you now know what VBA is, how to access the editor, and to write a very simple sub routine with the msgbox code. So what about the rest of VBA? How does it work? What does it mean? It's no double rainbow, but you can use these tutorials to help you understand the basics so you too can make your own game as cool as a double rainbow. Let's continue!


What is an object?
VBA is an object-oriented script, which means the code uses objects to perform an action or change part of an object. The entire collection of objects for a specific program such as PowerPoint, is called an Object Model or Object Library. You can see which Libraries you have access to by clicking on Tools > References from the VBA Editor window. You'll see there's a LOT of unchecked libraries available, and that's OK. You would need to know how to turn on an object model if you were doing something advanced such as working with Excel in your PowerPoint code, but I won't get into those details now. Just know for these tutorials, everything you need to work with is already set for you!

Objects in PowerPoint vary from slides, shapes, effects, toolbars, and so on. There are two different types of object members called Properties and Methods.

Properties describe a certain property of an object, such as height or width of a shape or textbox.Methods are the things an object can do, such as adding or deleting a shape. 

You can make a brand new shape using a method, and then change the properties to make it big or small, change the color, add text to it, change the font, etc.

Note on objects:
You may see the same methods or properties on various objects, however not all methods or properties are part of every type of object. But don't worry! As you are typing your code, you can see which objects you have available to you. More on that later.

Now, objects are hierarchical, or tree-like by nature. For example, if I wanted to locate my bed in my house, this is how the code might look like:
Code:
MyHouse.Floor(2).Room(1).Bed("Master")

As you can see, each object is part of the object above it, like a sub-object. Objects under MyHouse could be Floor, Garage, Basement, not Car or Fridge as those would be found further on Garage or Floor(1).Room("Kitchen") Not making sense? It's ok! The more you play with it, the more you'll get used to how to orient your objects. On to the editor!

Start by making a new subroutine, lets call this addshape.

To get to the shapes, we need to start big and work our way down to get to shapes. Start typing the following.

Code:
Sub addshape()
ActivePresentation.
End Sub

When you get to the dot, you'll see a pulldown of the next available objects associated with ActivePresentation.

Now, following the path to get to shapes, what is a PowerPoint Presentation made up of? Slides! So we'll type in Slides followed by another dot.

Code:
ActivePresentation.Slides.

You may have noticed that you don't see Shapes in the next pulldown. This is because right now the object "Slides" is referring to the whole collection of slides, and shapes is not an object member of Slides. To refer to a specific slide, say Slide 1 for example, we would need to tell the code that like so:

Code:
ActivePresentation.Slides(1).

Note: Calling out Slides(1) and Slides("1") are different. Slides(1) without the quotes refers to the slide that's in the first position of your presentation. Slides ("1") with the quotes refers to a specific slide named "1" (which probably doesn't exist, so it probably won't do anything!)

Now you should be able to see Shapes in the pulldown. Again, with Shapes, you would probably want to call out to a specific shape if you are going to be modifying an existing shape with parenthesis just like with Slides. Since we do not have an existing shape, we'll be adding our own. When you start typing Addshape, you'll see all the objects associated with Shapes. Then, you'll see a pulldown of all the shapes you can add. Let's add an Oval by using this code:

Code:
ActivePresentation.Slides(1).Shapes.Addshape msoShapeOval

When you have selected your shape, you'll see a contextual message pop up that will help you through the different parts to this object. You are currently typing in the bold section. To go to the next section, type a comma and a single space, and then you can modify the next section. For this example, Type refers to the type of shape, Left and Top refer to the X, Y position of the shape in points, and Width and Height are self-explanatory, also in points. Points are a unit of measurement used in VBA, not inches, feet, millimeters, centimeters, etc. 72 points = 1 inch (or 28.35 points = 1 cm for the rest of the world.) You don't have to fill in all the blanks every time you see these different options. If left blank, it will add a default Oval.



After you have written your code, test it out! Run your macro, and it should add your oval on the first slide just like that. If it didn't, go back and check your coding for spelling and any stray characters. 

Get in the habit of not only saving often, but testing your code often so you can easily detect where in your code to focus your troubleshooting.

Now, lets modify it with a macro!
Remember how I mentioned about the difference between Slides(1) and Slides("1")? Same is true with shapes. To call out a shape, you could use Shapes(1) or Shapes("shape name here") to call out your shape. But how do we know what the shape's name is? We can't simply ask "What's your name?" . . . "Ok fine, don't answer, square." . . . Shapes won't talk, and if they do, you need to seek immediate medical attention. For everyone else, we can look up their name on the Selection Pane, because shapes don't wear name tags.

Selection Pane
It's a good idea to have the selection pane open while coding, so you can see all the objects on your slide. You can get to it by clicking on the selection tool from the Home tab, on the far right side. Click on the pull down under Select, and select Selection Pane. You should probably see something like this:



In this example, my circle's name is "Oval 3"

So, if we want to change the width of the circle to make it more of an oval, we would do something like this:
Code:
ActivePresentation.Slides(1).Shapes("Oval 3").Width = 100

You can have this line in a new sub routine called Modify Shape, like this:

Code:
Sub ModifyShape()
ActivePresentation.Slides(1).Shapes("Oval 3").Width = 100
End Sub

Run macro ModifyShape, and watch the oval get a little wider. The width should now be approximately 1.5 inches.

Now lets add a second line of code. This time, we're going to add 50 points to the current height.
Code:
Sub ModifyShape()
ActivePresentation.Slides(1).Shapes("Oval 3").Width = 100
ActivePresentation.Slides(1).Shapes("Oval 3").Height = ActivePresentation.Slides(1).Shapes("Oval 3").Height + 50
End Sub

Run the ModifyShape macro, and you'll see the width stays the same, but the height gets larger. Click it again, and the width will stay the same, but the height will continue to grow. This is because you have told the width to be 100, but the height to be 50 more than where it is currently.

Don't know what the height or width of your shape is in points? Don't worry, I've got you covered! Try this code out for size:
Code:
Sub WhatIsMyHeight()
MsgBox ("The height of this shape is: " & ActivePresentation.Slides(1).Shapes("Oval 3").Height & " points.")
End Sub

Then, select your oval and apply the "WhatIsMyHeight" macro to it using Action Settings. Run your slideshow, and click on the shape and see what happens!

Note: Text as you would like it to appear is surrounded by a pair of double quotes. To combine text with another piece of data you would need to use the & symbol. You would need to remember this when you are going to have code that generates messages or text using data from your file.

So to review, we went over:
What a Method is, such as addshape, duplicate, delete
What a Property is, such as height, width
What the Selection Pane is and how to use it
How to use msgbox to help troubleshoot and view object properties

More to come on how to Modify objects in How to VBA 3!
View previous topicBack to topView next topic
Permissions in this forum:
You cannot reply to topics in this forum