PowerPoint Creative
Log In or Register to participate in PPC!

View previous topicGo downView next topic
Posts : 239
Join date : 2017-08-16

How to VBA - Lesson 1: How do you VBA?

on Tue Aug 22, 2017 11:11 pm
Hello out there, this will be my first attempt at forming what I hope would be a general but in-depth guide on how to do some programming with PowerPoint!

In this first lesson, we'll go over what is VBA, and where to go to make programming magic happen, and how to make a button run a macro.

So, what is VBA?
VBA stands for Visual Basic for Applications and is the programming language of PowerPoint, Excel and other Microsoft Office programs. VBA programming code tasks are commonly referred to as "macros."

To VBA or not to VBA?
You are going to have to ask yourself this very important question: "Do I really need what I want to accomplish as a VBA macro, or is there a simpler way such as hyperlinks and animations?

The reason you should ask this question, as not everything you do from now on has to be programmed. Don't forget what PowerPoint can do already for you, without you telling it how to do something it already knows how.

Allowing VBA to run
Does your PowerPoint settings allow you to run macros? If not, check out your Trust Center settings in PowerPoint options, and then go to the Macro Settings.

Don't be so quick as to allow ALL macros to run without your knowledge! VBA is a very powerful language that not only affects PowerPoint but all Microsoft products including the Windows OS and unfortunately, there are some not-so-nice people out there that use macros to do some nasty stuff with VBA. Open all macro-enabled files at your own risk.
Add Developer Tab to ribbon
Right click anywhere in the ribbon at the top, and select Customize Ribbon. From there, you'll see two columns of tools and the ribbons currently loaded onto your PowerPoint. By default, the Developer tab is checked off. If you check the box to add it and then click OK you will now be able to get to the VBA editor.

Here are some of the tools you get on the Developer tab:
Click on "Visual Basic" to open the VBA edit window.
Click on "Macros" to select and run a macro to perform a task.
Click on Macro Security as a quick shortcut to your Trust Center Settings for Macros.
You can manage Add-ins to run with your file.
You can add ActiveX form controls to PowerPoint and make your own form.

Learning the basics of VBA
Learning VBA is a lot like learning a foreign language because VBA IS a foreign language. You would need to learn proper syntax so that way you don't confuse VBA and cause it to error out. VBA, just like any programming language, is VERY literal and can and will error out easier than you think!

The macros you make are as smart as you code, it will not assume that "Well, looks like he forgot a comma here, so I'll treat this case as if he added a comma and I'll continue on." It will see a missing comma and stop with something like "Syntax error! Debug?" And then tell you "Hey, there's something weird with this line, I don't understand what you want me to do here." Luckily for you, the VBA editor does its best to try to keep you in check with some contextual pop ups as you start typing your code.

The VBA Editor Window
A quick overview of what you're looking at when the window pops up. At the top are your common tools. You'll see a Play, Pause, and Stop button which are used to run a macro from the editor window. Design mode is for making a fancy pop up form which I'll go over in another lesson.

You'll see a pane on the left side which will be your Project explorer window. You will see all PowerPoint files you have open, and their VBA code if they have any. To the right is where you'll see the code in the code window. The code window defaults to maximize the window, but you can make it a resizable window in this space so you can see other windows of code at the same time.

Back to the project explorer, right click on the name of your PowerPoint file, click on Insert, Module. The Module is where you will be storing all your code for now. It's a nice central location in your file for your code to be located. You might see your first line of code at the top for you: "Option Explicit" All this means, is all variables you want to use have to be declared. For example, I can't just say x = 1, I have to tell it "Hey, I'm going to use a variable of 'x' and it's going to be a number." I'll explain more of this later on.

Ok, that's enough background, let's start coding already!
Well now, aren't we a bit hasty? So you got your VBA Editor window open, module created, and ready to type in code. Let's do this, you are going to write your very first macro!

To start a new macro, you begin with giving a name to your macro, also known as subroutine (sub):

Sub MyFirstMacro()
This will automatically add the closing tag of:

End Sub
Everything you put in between these two lines will run in order from top to bottom and then stop with End Sub.

Note how the words "Sub" and "End" are in blue? Those are reserved words in VBA, and you can't use them as variables on their own. You'll see more reserved words pop up the more you code.

Now, let's create a little pop-up notification to appear with your macro. Add this line in between your Sub and End Sub lines:

MsgBox ("If you can read me, my first macro worked!")
If you started typing out "MsgBox" you'll see a contextual note that will guide you on how to properly use this command, with different command settings separated by commas. It will have a section bolded that will tell you which part of the command you are in.

Adding another macro
Start by typing "Sub SecondMacroName()" underneath the End Sub line, and watch what happens when you hit enter. You will see a horizontal break line separating your first macro with your second one you just started. VBA will have these lines to help visually tell you where each macro starts and stops. At the top right side of your code window is a pulldown with all of your subroutines you have created. You can select the subroutine you want to view and it will take you to the top of that subroutine. Neat.

Running your macro
To test this, have your cursor anywhere in between your two Sub lines and click on the Play button at the top.

You can also go back to your PowerPoint window and click on Macros from your Developer tab, and run "MyFirstMacro" and see what happens.

Assign your macro to a shape
Make a shape and place it anywhere on your slide. Give it some text so that we know that this is your macro button. Then, you'll want to go to Action Settings, under the Insert tab at the top. This is where you can tell what happens to the shape when you click it OR mouse over. You can tell it to go to a specific slide OR run a macro. We want it to run a macro.

Note It can also play a sound effect as well (.wav files only) Fortunately, when you tell it to play one of your wav files, it will stay embedded into your PowerPoint until no other objects are using that sound.

After you have assigned your macro to that shape, you can test it by running it in slideshow mode. Click on your button and watch what happens. You just made macro magic!

There you have it! The absolute basics of VBA in PowerPoint. Stay tuned for more VBA fun from yours truly, rusnakcreative.
View previous topicBack to topView next topic
Similar topics
Permissions in this forum:
You cannot reply to topics in this forum