Edit The Macro


The macro is edited in the Visual Basic Editor. Go to Tools Macro Macros to bring up the Macro window.

click to expand
Figure 17-4: Macro Window

Click the Edit button to bring up the VB Editor, with the new code showing. The code should look something like this:

 Sub CreateClientName() ' ' Adds client name to slides ' Macro recorded 1/20/2004 by Kathryn Jacobs ' ActiveWindow.Selection.SlideRange.Shapes("Rectangle 2").Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Characters(Start:=1, Length:=0).Select     With ActiveWindow.Selection.TextRange         .Text = "Dummy Partners"         With .Font             .Name = "Arial"             .Size = 44             .Bold = msoFalse             .Italic = msoFalse             .Underline = msoFalse             .Shadow = msoFalse             .Emboss = msoFalse             .BaselineOffset = 0             .AutoRotateNumbers = msoFalse             .Color.SchemeColor = ppTitle         End With     End With End Sub 

The first line, Sub CreateClientName, is the name of the macro. The last line, End Sub, tells VB the macro is finished.

The lines starting with a single quotation mark are comments. Comments explain what this macro is supposed to be doing. They are also a great place to note changes things in the code. Comments are good things. The better the comments, the less you need to remember about the code. Any time you do something tricky or unusual, add a comment about what you did and why.

Each of these areas will be in all macros created. They are the shell of the macro.

The section we are most interested in are the three lines starting with "ActiveWindow." (If you look at the code in the VB Editor, you will see all of the information is on one line. It word wraps here in the book due to its length.)

What's This "With" Stuff?

The VBA command "With" tells VBA the next few lines should be assumed to start with what comes after the With. These commands are commonly called "With clauses." It is a way to prevent having to repeat some of the content. Let's look at the first With clause in the macro:

 cith ActiveWindow.Selection.TextRange           .Text = "Dummy Partners" 

These two lines are saying "For the next line, assume the variable to the right of the equal sign really starts with ActiveWindow.Selection.TextRange." The ".Text" attribute is understood to be "ActiveWindow.Selection.TextRange.Text."

The other With statement in the code makes it clearer why you would want to use the With clauses. Instead of typing:

 ActiveWindow.Selection.TextRange.Text.Font 

in front of each of the next 10 lines, the With .Font gives the attributes for the font. With clauses not only make it easier to type and edit the code, once you get used to reading them, they make it easier to understand the code, too.

In VBA-speak, those periods are called separators because they separate one VBA term from another. You can use With clauses to repeat any set of terms, as long as you set your With clauses up to end where a separator would appear if you were typing the whole command or clause.

Back To The Code

This code is good, except it has a major gotcha in it. Because the recorder records exactly the steps you perform, it will always add the text you typed. Of course, we don't always want to add "Dummy Partners." We want to be able to set the new name to whatever we want.

To make this change, we need to use a variable for the new name. Variables are places VB can store data so you can reference it by a name instead of by the exact value.

To change the macro, insert a line in front of the first ActiveWindow line. In this line, type

 NewClientName = "Dummy Partners" 

The next step is to tell the macro to use the variable, NewClientName, instead of a fixed value in the existing code. To do this, replace Dummy Partners in the .Text line with the variable name, NewClientName. The first With clause should now look like this:

 With ActiveWindow.Selection.TextRange             .Text = NewClientName 

Now, we need to close and save the macro. You can do this via File Close and Return to PowerPoint, or by using Alt+Q. This will return you to the presentation. Save the presentation.

Next, do a test run of the presentation. On the slide, remove any text you added. To run the macro, do a Tools Macro Macros. Make sure the macro name is selected, then click Run. The screen will flash and the text Dummy Partners will appear in the title placeholder on the title slide.

What happens if the macro runs a second time? The name is added after the existing text. For this reason, always run this macro on a slide with no existing title text.

We are now going to record the steps to add the contact name and address. Repeat the recording process, adding the following text to the sub-title placeholder:

  • Linda Lyle

  • 2300 Southern Dr.

  • MyTown MM 67123

  • 555-666-8888

When recorded, the new macro, "CreateClientContact," should look like this:

 Sub CreateClientContact () ' ' Create the contact name, address, and phone number slide ' Macro recorded 1/20/2004 by Kathryn Jacobs ' ActiveWindow.Selection.SlideRange.Shapes("Rectangle 3").Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Characters(Start:=1, Length:=0).Select     With ActiveWindow.Selection.TextRange         .Text = "Linda Lyle" + Chr$ (CharCode:=13) + "2300 Southern Dr." + Chr$ (CharCode:=13) + "MyTown MN 67123" + Chr$ (CharCode:=13) + "555-666-8888"         With .Font             .Name = "Arial"             .Size = 32             .Bold = msoFalse             .Italic = msoFalse             .Underline = msoFalse             .Shadow = msoFalse             .Emboss = msoFalse             .BaselineOffset = 0             .AutoRotateNumbers = msoFalse             .Color.SchemeColor = ppForeground         End With     End With End Sub 

Wondering what "Chr$(CharCode:=13)" means? That is adding the hard return between the lines of text on the slide. As the text is changed to variables, we will create a variable for that too. Modify the new code to look like this:

 Sub CreateClientContact () ' ' Create the contact name, address, and phone number slide ' Macro recorded 1/20/2004 by Kathryn Jacobs ' ' Set up new line variable and contact information variables     NewLine = Chr$(CharCode:=13)     NewContactName = "Linda Lyle"     NewContactStreetAddress = "2300 Southern Dr."     NewContactCityStateZip = "MyTown MM 67123"     NewContactPhone = "555-666-8888" ' Find the place the stuff goes ActiveWindow.Selection.SlideRange.Shapes("Rectangle 3").Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Select ' Do the replace ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Characters(Start:=1, Length:=0).Select     With ActiveWindow.Selection.TextRange         .Text = NewContactName & NewLine & NewContactStreetAddress & NewLine & NewContactCityStateZip & NewLine & NewContactPhone         With .Font             .Name = "Arial"             .Size = 32             .Bold = msoFalse             .Italic = msoFalse             .Underline = msoFalse             .Shadow = msoFalse             .Emboss = msoFalse             .BaselineOffset = 0             .AutoRotateNumbers = msoFalse             .Color.SchemeColor = ppForeground         End With     End With End Sub 

For the remainder of this chapter, I leave the addition of comments to you.

What About Formatting My Text Pieces?

The code we are creating in this chapter is designed to use the text formatting as defined in the template and on the existing slides. That's what each With .Font clause does. Since Larry is working with an existing, formatted presentation, this isn't a problem; each of his With clauses will pick up the font information from the template. However, to change the formatting on the slides, change the text attribute values in the code.

To make the font different, change Arial to another font name. Same goes for the size and the other attributes. If you really want to be slick, create variables for the font attributes and make the changes to the variables instead of after each equal sign.

Creating A Macro From Scratch

Repeat the recording process for the goals to create the remaining macros. These macros will be much like the ones we just did. However, when you get ready to run these macros, you will want PowerPoint to move from one slide to the next.

The macro to change from one slide to the next is not one that will easily record. Instead, we are going to create the macro from scratch. You can create this macro from any slide in the presentation. Go to Tools Macro Macros and type in the name for the new macro, ChangePage. As soon as you start to type, a Create button will appear. Clicking the button will bring up the VB Editor and place the cursor between the comments and the End Sub command. You are ready to create the macro.

First, add a comment describing what the macro is going to do Next, move the cursor to the first line after the comments. Type in the code you

 Sub ChangePage() ' Change from one slide to the next ' Macro created 1/20/2004 by Kathryn Jacobs '    ActiveWindow.View.GotoSlide Index:=ActiveWindow.Selection.SlideRange.SlideIndex + 1 End Sub 

As you type, a box comes up containing various VBA objects. These objects work together to make the instructions that are the macros. Objects are separated by periods, commas, parentheses and operators. As you type, the VBA environment will try to guess what object you are looking for. When it shows the object you want, you can type the separator to select the object you want.

Since the editor knows which objects can go with which other objects, the guesses are generally pretty close to what you want. Taking advantage of the guesses can save you keystrokes while typing the code and help make the code more likely to run correctly.

When you have finished typing in the lines of code above, close the VB editor and return to PowerPoint. Do a test run of the macro to make sure you move from the current slide to the next slide. If you run the macro from the last slide in the presentation, it will give you an error. Why? Because there is no slide number plus one in the presentation. If you get any other errors when you run the macro, go back to the editor and check for and correct any typographical errors.

When the code runs cleanly (that's computer speak for when it works), you are ready to create the rest of the macros.

Creating A Macro From Another Macro

To easily create the goals macro, we are going to copy the code from the contact information macro and adapt it to our use.

Give the new macro a name and click Create. (I used CreateGoals in this example.) Scroll up through the code window to find the contact information macro. Select the comment and code lines (not including the End Sub), copy them, move back down to the CreateGoals macro and paste them.

Edit the copied lines so the macro looks like this:

 Sub CreateGoals() ' ' Macro created 1/20/2004 by Kathryn Jacobs ' ' Set up new line variable and contact information variables     NewGoalLine = Chr$(CharCode:=13)     NewGoal1 = "Goal 1: My First Goal"     NewGoal2 = "Goal 2: My Second Goal"     NewGoal3 = "Goal 3: My Third Goal"     NewGoal4 = "Goal 4: My Fourth Goal" ' Find the place the stuff goes ActiveWindow.Selection.SlideRange.Shapes("Rectangle 3").Select ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Select ' Do the replace ActiveWindow.Selection.ShapeRange.TextFrame.TextRange .Characters(Start:=1, Length:=0).Select     With ActiveWindow.Selection.TextRange         .Text = NewGoal1 & NewGoalLine & NewGoal2 & NewGoalLine & NewGoal3 & NewGoalLine & NewGoal4         With .Font             .Name = "Arial"             .Size = 32             .Bold = msoFalse             .Italic = msoFalse             .Underline = msoFalse             .Shadow = msoFalse             .Emboss = msoFalse             .BaselineOffset = 0             .AutoRotateNumbers = msoFalse             .Color.SchemeColor = ppForeground         End With     End With End Sub 

Test and correct the macro until it runs the way you want it to.

Making The Macros Easier To Run

As these macros are written, Larry has to edit each macro to make his changes. That really isn't any easier than what he was doing. Larry would like the macros to ask him what information should be on the slides and automatically place his answers in the right places.

The way to do this is to use input boxes. Input boxes are dialogs that pop up when you run the macro and ask you to provide information you want the macro to use.

To create an input box in a PowerPoint macro, use the InputBox function. InputBox is used to get input from users by opening a message box where they can type in text. This information is then passed to a variable.

We are going to change the variables in the macros we just created to use Input boxes instead of having the text hard-coded into the macro. ("Hard-coding" is the term for when the value for a variable is set in the code and the only way to change it is to edit the code.)

To set up the input boxes, edit the CreateClientName macro. Replace

 NewClientName = "Dummy Partners" 

with the following code:

 NewClientName = InputBox("What is the name     of the client company?") 

Test the change and make any needed corrections. Once it is right, you can make the same kind of changes within each of the other macros by replacing the quoted text with the InputBox function.

You don't need to edit each macro separately. The VBA editor will move between macros in a single edit session. If you do edit more than one macro at a time, be sure to test each one thoroughly to make sure they all work at the end.

When creating the input boxes for the goals, make sure the goals still say which goal they are. You can either place a reminder in the prompt for the input box and have "Goal 1:" typed before the goal, or you can add "Goal 1:" and so on to each goal line. The two ways these variables can look are:

 NewGoal1 = InputBox("What is the first goal     of this project? (Format is: Goal 1: Goal)") 

or

 NewGoall = "Goal 1: " & InputBox("What is     the first goal of this project?") 



Kathy Jacobs On PowerPoint
Kathy Jacobs On PowerPoint
ISBN: 972425861
EAN: N/A
Year: 2003
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net