3. Getting Started with VBA

In previous blog you learned some basic of VBA. This Blog will explain how to access the VBA Editor, how to write simple scripts in VBA, how to attach those scripts to buttons and objects, and how to protect your scripts with a password. When you complete reading this blog, you will know the basics of writing a script and using it in a PowerPoint presentation, and you will be all ready to do some interesting things with VBA.

  • Accessing the VBA Editor

Once you start a PowerPoint project, you get into VBA by holding down the ALT key and hitting the F11 key (Option-F11 on a MacOS). Alternatively, go to the Developer tab of the Ribbon and click on Visual Basic. If you don’t have the Developer tab, go back to blog#1 to review how to get it.

At this point, you should see two small windows on the left (the Project window and the Properties window) and a large blank area on the right of the screen.  

You also might not see either the Project window or the Properties window; you can get them back by choosing them from the View menu.

Choose Module from the Insert menu , and you will get a window in the blank area. The window probably will be named “Module1”. This is where you will write your VBA procedures.

While we are here, let’s write one VBA code. Type the following:

Sub SayHello()
     MsgBox "Hello World"
End Sub

Note that the computer will type the End Sub and the parentheses for you if you just hit Return or Enter on your keyboard after typing “Sub SayHello” .

Now go to the Run menu , and select Run Sub/UserForm . You should get a message box that says Hello World!

Congratulations! You have just written and executed your first VBA procedure successfully. Click the OK button, and you can do some more.

  • What If It Didn’t Work?

When you type code, it doesn’t always work the first time (or the second time or the third time or . . .), i will give lots of great ideas in my upcoming blogs for fixing what’s wrong with your code; but for now here’s a quick tip. After typing some code, make sure that it starts with Sub and ends with End Sub. The next most common mistakes are: putting a space between Say and Hello on the first line (it should be one word) and getting the quotes wrong around Hello World! (they’re regular, ordinary double quotes, and when you have an open quote, you need a close quote). Check everything carefully, and you’ll be able to get this to work.

  • Be a Scripter: Change Things in Quotes

If your goal is to be a programmer, you should try to understand every detail in every line of code. If your goal is to be a scripter, you should have a basic understanding of what the code does, and you should try to understand as much of the details as you can. But the most important thing you can understand as a scripter is what you can change. One clue is that you can change most things that are in quotes. In this code, the word Hello World! is in quotes, and “Hello World!” popped up on the screen when you ran the procedure. If you want something else to pop up on the screen (like “Hello, PowerPoint!” or “My First VBA code” or “My Name is, Bla bla”) then put that in the quotes in your code instead. Give it a Try..!!

  • Subroutine, Procedure, or Macro

What do we call the code that you just wrote (the stuff that starts with Sub and ends with End Sub )? The word Sub is short for subroutine. It is commonly referred to as a procedure, and these terms will be used in all of my upcoming blog posts. It could also be referred to as a macro, and you will see that term in the Developer tab of the Ribbon (refer image below). In short, any of those terms is correct and are equivalent.

  • Attach Your VBA Script to a PowerPoint Button

Now that you have a Macro written, you may want to access it from within PowerPoint. You can do this by assigning/attaching this Macro to a button (or any drawing shape that you want).

Assuming that you are still at VBA editor, you can click on old style PowerPoint icon on top left corner to Return back to Microsoft PowerPoint (refer image below).

Note: You can also close the Visual Basic Editor by clicking on the X in the upper right-hand corner of the screen. Don’t worry about losing your VBA scripts when you close the editor. Your VBA scripts are part of your PowerPoint presentation. When you save your presentation, your scripts will be saved with it. When you return to the editor, your scripts will still be there.

Now to Attach Your VBA Script to a PowerPoint Button, you must first add an action button to your slide. You will find action buttons under shapes menu, at the bottom most section.

You can pick any one of the action buttons and draw it on the slide. As soon as you finish drawing the button, by dragging the mouse to form the button or just clicking where you want the button to appear on the slide, you will be presented with the Actions Settings dialogue box . Choose Run Macro , and select SayHello (the name of the procedure you just wrote) as the macro to run. Click OK .

Buttons are only active in Slide Show View, so go to Slide Show View by pressing F5 key on your keyboard (Note: If you have more than one slide, then press ‘Shift+F5’ keys to start slide show from current slide). Now, click on your action button, and you should get the same “Hello World!”  message you got earlier when running your Macro.

Now go back to Normal View (also known as Edit View) by hitting the Escape key on your keyboard. To finish your button, right-click on it and choose Edit Text from the fly-out menu or select the button and just start typing. You can now add text to describe what your button does. This text will show up on the button, so users will know what they are clicking when they click your button. For this button, you might type “Say Hello”.

  • Attach Your VBA Script to any Object/Shape

Not just to a button, you can assign your VBA script to any object you want. Use the drawing tools to draw a shape (Choose Shapes menu from the Insert tab of the Ribbon),  Once you have drawn the shape, click on it to select it. Now choose Action from the Insert tab of the Ribbon, you will get the same dialogue box as in case of action button, you can choose Run Macro and the SayHello macro, exactly as you did earlier. Now you can click on the drawn object just like you can click on the button.

This method works for any PowerPoint object, not just the ones you draw yourself. You can insert clip arts, pictures from other sources, and even make text in your slide clickable by highlighting the text and following the same steps.

  • Securing Your VBA Script

In many circumstances, you may not want others to see your VBA Script (Example: You have written VBA script for a Quiz game and you don’t want to reveal their answers) and It is very easy to protect your VBA code with a password. Return back to VBA Editor where you edit the VBA code, select VBAProject Properties from the Tools menu and click on the Protection tab. Check the box that is labeled Lock project for viewing , type a password in the “Password” box, and type the same password in the “Confirm password” box and then click Ok.

Now, whenever you want to view or edit the VBA code, you will be asked to type this password. Don’t forget it, else even you will not be able to access your own project.!!

Leave a Reply