Editing a Macro with Visual Basic
When you're recording a macro, errors inevitably occur. Perhaps you meant to click the Bold button and clicked Italic instead, so you had to turn italics back off again. Or, maybe you opened the wrong menu.If the macro performs its function, even with the errors, you might just leave it alone. For example, it doesn't hurt anything if the macro turns Italics on and then off again because the end result is that it's off. However, if there are a lot of mistakes in a complex macro, it can take a little longer to run than normal. Some people, too, are sticklers for efficiency and can't stand the thought of their macro being longer and more convoluted than necessary, regardless of performance issues.To edit a macro, you use the Visual Basic Editor. This is a complex application that might seem intimidating to the non-programmer, but as long as you stick to editing with it, you should be fine.
Note | More intrepid types with Visual Basic experience might choose to try their hand at writing macros from scratch. Be aware, however, that Visual Basic for Applications (VBA), which is what PowerPoint uses, is slightly different from regular Visual Basic. To familiarize yourself with the version of Visual Basic within PowerPoint, I recommend that you create some fairly complex macros by recording, and then examine them in the Visual Basic Editor to see how it structures command lines. |
To edit a macro, choose Tools⇨Macro⇨Macros or press Alt+F8 to open the Macro dialog box. Click the macro and click the Edit button. The Visual Basic Editor opens with your macro in it. If you have more than one macro recorded in the same presentation, all of them appear in the Module 1 window, regardless of which one you chose initially (see Figure 19-6).
Figure 19-6: Editing a macro in the Visual Basic Editor.
Note | The BoldltalicUnderline macro in Figure 19-6 originally had a line at the beginning that selected text, but I removed it. I'll explain why-and how you can do the same in your own macro, if desired-later in the chapter. |
To remove a line from the macro, highlight it and press Delete. You can figure out what a line does by reading it carefully. For example, the following line turns the Bold attribute on by settings its value to True:
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
When you are finished editing the macro, choose File⇨ Close and Return to Microsoft PowerPoint or press Alt+Q. Your changes are automatically saved.
Some VBA Fundamentals
Visual Basic is a rich programming language, and the version of it that works within Office applications enables you to write complex macros and even build mini-applications with dialog boxes that open with PowerPoint. This is way beyond what most business users would ever consider, but you may nevertheless be curious about the editor.The Visual Basic Editor window has three panes: Project, Properties, and Code (see Figure 19-6). The Project window shows a hierarchy of the modules in the presentation. All the macros you record are stored in Modulel, so you don't have to worry about this. The Properties panel shows the properties for the module. This pane is FYI-only too, as far as beginners are concerned.The main panel that you work with is the Code window, the big one. It contains the lines of programming code that make up your macro.Here are some things to note about the code:
Each macro begins with the word Sub, followed by the macro name.
Each macro ends with the words End Sub.
Each command of the macro is on its own line.
Each command in the macro narrows down what's being done through a series of words separated by periods. For example, in the BoldltalicUnderline macro in Figure 19-6, ActiveWindow.Selection.TextRange.Font narrows down the activity to apply only to the font properties of the selected text range in the active window.
Some lines begin with an apostrophe, such as the Macro recorded... line in Figure 19-6. These lines are comments and are ignored when the macro runs. If you want to temporarily disable a line of the macro for troubleshooting purposes, you can add an apostrophe in front of it.
Note | If you are interested in learning more about VBA, a good first place to start is the Help system. Don't think, however, that you can teach yourself prog ramming just by reading the Help system. It's good, but the help there is slanted toward people who already know something about the topic. You won't find many easy explanations of how programming works. If you want to write your own VB macros from scratch, take a Visual Basic class at your local community college. |
Here are a few additional Web resources for working with VBA in PowerPoint, all from the awesome officeone.mvps.org site that I've already referenced many times in this book:
VBA Controls Assistant. If you are a VB wizard, you might have some VBA controls created. To use them in a presentation, check out the add-in, which supports VBA TextBox, VBAListBox, VBA ComboBox, and TreeView (http://officeone.mvps.org/ppvba/ppvbal).
Event Generator. Normally the event handler for events like OnPresentationClose(), OnPresentation0pen(), and so on can reside only in add-ins. The Event Generator add-in redirects many of the common program events to the presentations. Use for VBA programmers (http://officeone.mvps.org/eventgen/eventgenl).
Quiz example. Here's an article that shows how to create a quiz using VBA: www.mvps.org/skp/ppt00031.
Tic Tac Toe. Here's another PowerPoint demo using VBA: www.mvps.org/skp/downloads/tictactoe.zip.
Removing Text Selection From a Macro
Here's a quirk involving macros that apply text formatting, and how to fix it.When recording a macro that formats text, you must either select some text before beginning the recording or select some text during the recording. If you don't, the macro will not record correctly.However, selecting text has consequences you may not want. For example, suppose you select characters 10 through 14 of the text in a text box, and then record a macro that makes it bold. When you run that macro in the future, it will select characters 10 through 14 in the active text box and then make them bold. You probably don't want the selection to be part of the macro, though, right?Here's how to fix that. Go into the Visual Basic Editor and either delete or comment out (by adding an apostrophe at the beginning) the code that specifies the selection.For example, if the original was like this:
ActiveWindow.Selection.Shape Range.TextFrame.Text Range.Characters
(Start:=10. Length:=5).Select
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
you would delete that first line, so all that remains is this:
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
Now when you run the macro, it will simply apply to whatever text you have already selected before running it, and the macro won't try to make any selection of its own.
Selecting the Right Steps to Record: An Example
In the course of examining your macros in the Visual Basic Editor, you will get an eye-opener as to the inner workings of PowerPoint. For example, you will see that commands that appear identical in everyday use are actually not identical at all. When you run into such cases, you may need to rethink the way you have a particular macro recorded, and possibly rerecord it.Take, for example, the process of making some text bold, from the preceding section. One way of recording "bolding" is to record the clicking of the Bold button on the toolbar. That action creates this line in the macro:
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
(It also creates a line that selects the text, as described in the preceding section, but I have deleted it already here for simplicity.)Another way of making text bold is to choose Format⇨ Font, click Bold, and click OK. Seems the same, right? But when you record that method, you are also recording all the other settings in the Font dialog box as well, so it would look something like this:
With ActiveWindow.Selection.TextRange.Font
.Name = "Arial"
.Size = 32
.Bold = msoTrue
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With
This is called a "With" statement. Rather than repeating the text ActiveWindow.Selection.Text Range.Font on every line, it places it at the top of the statement and then just lists what has changed at the end of it on subsequent lines. Another way to have written this would have been:
ActiveWindow.Selection.TextRange.Font.Name = "Arial"
ActiveWindow.Selection.TextRange.Font.Size = 32
ActiveWindow.Selection.TextRange.Font.Bold = msoTrue
ActiveWindow.Selection.TextRange.Font.Italic = msoFalse
ActiveWindow.Selection.TextRange.Font.Underline = msoFalse
ActiveWindow.Selection.TextRange.Font.Shadow = msoFalse
ActiveWindow.Selection.TextRange.Font.Emboss = msoFalse
ActiveWindow.Selection.TextRange.Font.BaselineOffset = 0
ActiveWindow.Selection.Text Range.Font.AutoRotateNumbers = msoFalse
ActiveWindow.Selection.Text Range.Font.Color.SchemeColor =
ppForeground
The dialog box method recorded settings for font, size, italic, color, and many other settings that happened to be in effect on the text that I selected when I recorded the command. I didn't intend for those settings to be recorded, but there they were.There are several possible solutions. I could delete the macro and rerecord it using the Bold button method, or I could edit the With statement to remove any of the lines that I didn't want to keep. For example, I could remove all the lines except the one that makes the text bold, so it would look like this:
With ActiveWindow.Selection.TextRange.Font
.Bold = msoTrue
End With