• No results found

In the above ex amples, our scripts to hide and show ob jects specified which slides con tained the ob jects. Sometimes you want an action to affect the current slide with out re gard to which number slide it is. For ex ample, you might want to show an ob ject on the current slide or add some text to a text box on the cur rent slide or change the color of a menu item on the current slide. This is particularly useful when you want to write one pro cedure that will work on several slides. For ex am ple, if ourDoingWell procedure re vealed a star and each slide had a star with the same name or num ber, we could add a line to DoingWell to show the star on the current slide. One DoingWell pro cedure would be used for all the slides.

Sometimes you might want an action to affect an other slide. You might be on one slide but wish to have something change on an other slide, as we saw in ourIni tial ize pro cedure. In the Ini tial ize ex ample, we wanted the slide to be set up prop erly before the user got there, so the change happened to the other slides when the user was on the first slide. We also want changes to happen before we arrive at a slide. If we hide our star (or change text on a slide or change the color of menu items) just as we ar rive at a slide, the hid den ob ject will be on the screen for a split second be fore disappearing. In some cases, this might not be a big prob lem, but if the action is to hide the an swer to the ques tion, this could be very important.

Anything af fecting the cur rent slide will start with

ActivePresentation.SlideShowWindow.View.Slide

So, for example, if you want to hide shape num ber 7 on the cur rent slide, you would use this line:

ActivePresentation.SlideShowWindow.View.Slide.Shapes(7).Vis i ble = False

Anything af fecting an other slide will start with

ActivePresentation.Slides(NUM)

whereNUM is replaced by the num ber of the slide. So, for ex ample, if you want to hide shape number 7 on slide number 2, you would use this line:

ActivePresentation.Slides(2).Shapes(7).Vis i ble = False

Any ex pression throughout this book that uses a statement to af fect the cur- rent slide can be changed to use a statement for an other slide, and any expression that uses a state ment to af fect an other slide can be changed to use a state ment for the cur rent slide.

Adding PowerPoint Ob jects

In the previous sections, we hid and showed any ob jects that we wanted af- ter using nor mal PowerPoint drawing features to create the ob jects. Any ob ject that you can create with PowerPoint drawing tools you can create and manipu- late with VBA. In fact, you could use VBA to create your en tire pre sentation, in - cluding all the slides, all the but tons, all the shapes, and all the text. For al most all your pur poses, you are better off creating the shapes with PowerPoint draw- ing tools and us ing VBA to hide and show them, but in Chapters 7 and 10, we’ll see ex amples that cre ate slides and add shapes to them using VBA.

The prob lem with adding shapes is that they can collect in your presenta- tion and can be hard for you to de lete. For example, consider the star we hid and showed earlier in this chap ter. In stead of hid ing it and show ing it, we could use VBA to create it. The problem is that once the shape is cre ated, it is part of the presentation. Un less you are careful, you will have that shape (and pos sibly sev- eral cop ies of that shape) in corporated into your presentation. You can de lete the shape when you are done, but this is an ex tra thing to track and not generally worth the effort.

If you are not dis suaded from add ing shapes and would like to try it, you can try this ex ample. If you heed my warnings, you’ll skip this, go on to the next sec- tion, and only come back to this to un derstand the examples in Chapters 7 and 10.

Let’s add a sim ple square in the middle of the screen:

Sub AddRectangle() Dim myShape As Shape

Set myShape = _

ActivePresentation.SlideShowWindow.View.Slide.Shapes. _ AddShape(Type:=msoShapeRectangle, Left:=100, Top:=100, _ Width:=200, Height:=200)

myShape.Fill.ForeColor.RGB = vbRed

myShape.TextFrame.TextRange.Text = "Hello" End Sub

This looks complicated, but it is not as complicated as it looks. This procedure does three things: It cre ates the rectangle, it turns it red, and it puts the word “Hello” in side. Let’s take it line by line:

Dim myShape As Shape

We are going to cre ate a shape, so we create a variable to hold that shape. That way, once the shape is cre ated, we can re fer to it later in the procedure. Next we create the shape:

Set myShape = _

ActivePresentation.SlideShowWindow.View.Slide.Shapes. _ AddShape(Type:=msoShapeRectangle, Left:=100, Top:=100, _ Width:=200, Height:=200)

ActivePresentation.SlideShowWindow.View.Slide gives us the cur rent slide. Shapes gives us the shapes on the slide, and the AddShape

method is used to add a shape to the shapes on the slide. Now, ev erything be - tween the pa rentheses is simply telling you about the shape:

• TheType is what shape you are cre ating:msoShapeRectangle for a rect an gle.

Left and Top are the lo cation on the screen of the top left corner of the shape

Width and Height are how wide and tall the shape is.

Some other shapes you might use in stead of msoShapeRectangle are:

msoShape4pointStar msoShapeIsoscelesTriangle msoShape5pointStar msoShapeLeftArrow msoShape8pointStar msoShapeLightningBolt msoShapeBalloon msoShapeMoon msoShapeBentArrow msoShapeNoSymbol msoShapeBentUpArrow msoShapeOctagon msoShapeCross msoShapeOval msoShapeCube msoShapeParallelogram msoShapeCurvedDownArrow msoShapePentagon msoShapeCurvedLeftArrow msoShapeRectangle msoShapeCurvedRightArrow msoShapeRightArrow msoShapeCurvedUpArrow msoShapeRightTriangle msoShapeDiamond msoShapeRoundedRectangle msoShapeDonut msoShapeSmileyFace msoShapeDownArrow msoShapeSun msoShapeHeart msoShapeTrapezoid msoShapeHexagon msoShapeUpArrow

Try re plac ing msoShapeRectangle with some of the other shapes from this list.

Finally, we set some properties of the shape. Since the shape is stored in the vari able myShape, we can use myShape to manipulate some of the shape’s prop er ties:

myShape.Fill.ForeColor.RGB = vbRed

This line takes the shape we just cre ated and stored in the variablemyShape and adjusts its color. This looks com plicated, but you just have to remember that if you want to change the color of a shape, you need to adjust the

.Fill.ForeColor.RGB. Af ter the equal sign is the color we want. There are many ways to specify the ex act color, but you can use the fol lowing ba sic col ors:

vbBlack, vbRed,vbGreen, vbYellow,vbBlue,vbMagenta, vbCyan, and

vbWhite.

Shapes can also have words in them. If you want to set the text in the shape to “Hello,” use the fol lowing line:

myShape.TextFrame.TextRange.Text = "Hello"

This is simply a long way of saying that the text in this shape should be set to “Hello.”

Adding ob jects can be useful, especially if you want the user to make sig- nificant changes to the pre sentation. In the ex ample in Chapter 10, the user adds slides to the pre sentation. These slides be come part of the presentation, and there are an un determined num ber of them (every user that goes through the pre senta- tion can add slides to it). In most cases, how ever, you will have a few shapes that you have determined in ad vance. Rather than creating those shapes in VBA, you would do better to create them in PowerPoint and hide and show them with VBA. This will prevent your presentation from get ting cluttered with extra shapes when a user hits a but ton too many times and adds several extra shapes.