Working with PowerPoint Slide Objects PowerPoint presentations consist of a series of slides. In PowerPoint VBA, a slide is a Slide object that contains a number of properties and methods that you can wield in your code. These include options for setting the slide's layout, specifying the transition effect, and copying and deleting slides. The next few sections discuss these and other slide techniques. Specifying a Slide To work with a slide, you need to specify a Slide object. For a single slide, the easiest way to do this is to use the Slides object. Slides is the collection of all the slides in a particular presentation. To specify a slide, either use the slide's index number (where 1 represents the first slide in the presentation, 2 the second slide, and so on), or enclose the slide name in quotation marks. For example, if Slide1 is the first slide, the following two statements would be equivalent: ActivePresentation.Slides(1) ActivePresentation.Slides("Slide1") If you need to work with multiple slides (say, to apply a particular layout to all the slides), use the Range method of the Slides object: Presentation .Slides.Range( Index ) Presentation | The Presentation object that contains the slides. | Index | An array that specifies the slides. | For the Index argument, use VBA's Array function with multiple instances of slide index numbers or slide names . For example, the following statement specifies the slides named Slide1 and Slide2: ActivePresentation.Slides.Range (Array("Slide1","Slide2")) tip | To work with every slide in the presentation, use the Range method without an argument, as in this example: ActivePresentation. Slides.Range You can also use the Presentation object's SlideMaster property to work with the slide master. This will change the default settings for every slide in the presentation. | Creating a New Slide After you've created a presentation, you need to populate it with slides. To insert a new Slide object into a presentation, use the Add method of the Slides collection: Presentation .Slides.Add( Index, Layout ) Presentation | The Presentation object in which you want to add the slide. | Index | The index number of the new slide within the Slides object. Use 1 to make this the first slide; use Slides.Count + 1 to make this the last slide. | Layout | A constant that specifies the layout of the new slide. PowerPoint defines over two dozen constants, including ppLayoutText (for a text-only slide), ppLayoutChart (for a chart slide), ppLayoutBlank (for a blank slide). Look up the Add method in the VBA Help system to see the full list of constants. | The following statements add an organization chart slide to the end of the active presentation: With ActivePresentation.Slides .Add Index:=.Count + 1, Layout:=ppLayoutOrgchart End With Inserting Slides from a File Instead of creating slides from scratch, you might prefer to pilfer one or more slides from an existing presentation. The InsertFromFile method lets you do this. It uses the following syntax: Presentation .Slides.InsertFromFile( FileName, Index, SlideStart, SlideEnd ) Presentation | The Presentation object in which you want to add the slides. | FileName | The name of the file (including the drive and folder) that contains the slides you want to insert. | Index | The index number of an existing slide in Presentation . The slides from FileName will be inserted after this slide. | SlideStart | The index number of the first slide in FileName that you want to insert. | SlideEnd | The index number of the last slide in FileName that you want to insert. | For example, the following procedure fragment inserts the first five slides from Budget.ppt at the end of the active presentation: With ActivePresentation.Slides .InsertFromFile _ FileName:="C:\Presentations\Budget.ppt", _ Index:=.Count, _ SlideStart:=1, _ SlideEnd:=5 End With | If you specify multiple slides using the Range method described earlier, PowerPoint returns a SlideRange object that references the slides. This object has the same properties and methods as a Slide object, so you can work with multiple slides the same way that you work with a single slide. | Slide Object Properties To let you change the look and feel of your slides, PowerPoint VBA offers a number of Slide object properties. These properties control the slide's layout, background, color scheme, name, and more. This section runs through a few of the more useful Slide object properties. Slide .Background ” Returns or sets the background of the specified Slide . Note that this property actually returns a ShapeRange object. (See "Dealing with Shape Objects" later in this chapter.) You normally use this property with the slide master to set the background for all the slides in the presentation. For example, the following statements store the slide master background in a variable and then use the Shape object's Fill property to change the background pattern for all the slides in the active presentation: Set slideBack = ActivePresentation.SlideMaster.Background slideBack.Fill.PresetGradient _ Style:=msoGradientHorizontal, _ Variant:=1, _ PresetGradientType:=msoGradientFire If you just want to change the background for a single slide, you must first set the slide's FollowMasterBackground property to False, like so: With ActivePresentation.Slides(1) .FollowMasterBackground = False .Background.Fill.PresetGradient _ Style:=msoGradientHorizontal, _ Variant:=1, _ PresetGradientType:=msoGradientFire End With Slide .FollowMasterBackground ” As mentioned earlier, this property returns or sets whether or not the specified Slide uses the same Background property as the slide master. Set this property to False to set a unique background for an individual slide. Slide .Layout ” Returns or sets the layout for the specified Slide . Again, see the VBA Help system for the full list of layout constants. Slide .Master ” Returns the slide master for the specified Slide . The following two statements are equivalent: ActivePresentation.SlideMaster ActivePresentation.Slides(1).Master Slide .Name ” Returns or sets the name of the specified Slide . Slide .Shapes ” Returns a Shapes collection that represents all the Shape objects on the specified Slide . Slide .SlideShowTransition ” Returns a SlideShowTransition object that represents the transition special effects used for the specified Slide during a slide show. The Juggling Application: Creating the Slides Listing 9.3 shows the AddJugglingSlides procedure, which adds four slides to the Juggling presentation (represented, remember, by the pres variable) and then uses the SlideMaster object to set the default background for the slides. Listing 9.3. A Procedure That Adds the Slides to the Juggling Presentation and Formats Them Sub AddJugglingSlides() Dim i As Integer With pres With .Slides ' ' Add the opening slide ' .Add(Index:=1, Layout:=ppLayoutTitle).Name = "Opener" ' ' Now add the slides for each step ' For i = 1 To 4 .Add(Index:=i + 1, Layout:=ppLayoutTitle).Name = _ "Juggling" & i Next i End With ' ' Set the background for all the slides ' .SlideMaster.Background.Fill.PresetGradient _ Style:=msoGradientHorizontal, _ Variant:=1, _ PresetGradientType:=msoGradientNightfall End With End Sub Slide Object Methods PowerPoint VBA defines a few Slide object methods that let you cut, copy, paste, duplicate, export, select, and delete slides. I don't expect that you'll use these methods very often, so I won't discuss them in detail here. All are straightforward, however, so you should be able to figure them out from the VBA Help system. |