Microsoft Office 2000/Visual Basic Programmer's Guide |
By default, PowerPoint names slides by using the convention Sliden, where n is a number representing the location of the slide at the time it was added to the Slides collection. You can specify your own name for a slide by setting the Slide object's Name property.
There are four ways to access a Slide object in the Slides collection:
The following code sample illustrates three ways to return the third Slide object in the current presentation and a way to return the currently selected slide:
Dim sldCurrentSlide As PowerPoint.Slide
' Using the slide's index value.
Set sldCurrentSlide = ActivePresentation.Slides(3)
' Using the slide's name.
Set sldCurrentSlide = ActivePresentation.Slides("Slide3")
' Using the FindBySlideID method, where lngSlide3 contains the SlideID
' property for the third slide.
Set sldCurrentSlide = ActivePresentation.Slides.FindBySlideID(lngSlide3)
' Using the SlideIndex property to return the currently selected slide.
' This sample shows how to determine if a single slide is currently selected.
If ActiveWindow.Selection.SlideRange.Count = 1 Then
Set sldCurrentSlide = ActivePresentation _
.Slides(ActiveWindow.Selection.SlideRange.SlideIndex)
End If
If you want to work with a group of Slide objects, perhaps to apply consistent formatting to the slides, you can use the Slides collection's Range method. The Range method returns a SlideRange object representing one or more Slide objects in a presentation.
If you use the Range method without an argument, the method returns a SlideRange object that contains all the Slide objects in a presentation. To see an example of this, see the PresentationView procedure in the modRunPPtPresentation module in PowerPointSamples.dot in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder on the Office 2000 Developer CD-ROM.
You use the Range method's Index argument to specify one or more Slide objects to include in the SlideRange object returned by the method. If the argument is a single integer, the method returns a SlideRange object for the Slide object whose index value matches the integer. For example, the following sample returns a SlideRange object representing the third slide in the current presentation:
Dim sldCurrSlide As PowerPoint.SlideRange
Set sldCurrSlide = ActivePresentation.Slides.Range(3)
In addition to using the Index argument, you can also use the VBA Array function as an argument to the Range method in order to return a SlideRange object containing multiple Slide objects. The Array function uses a comma-delimited list of values to be included in the array. When used as an argument to the Range method, the comma-delimited list should contain the index values or names of the slides you want to include in the SlideRange object returned by the method. The following sample shows how to use the Array function to return a SlideRange object containing the first four slides with even-numbered index values:
Dim sldCurrSlides As PowerPoint.SlideRange
Set sldCurrSlides = ActivePresentation.Slides.Range(Array(2,4,6,8))
The next sample illustrates how to use the Array function to return a SlideRange object that is a collection of specific named slides in a presentation:
Dim sldCurrSlides As PowerPoint.SlideRange
Set sldCurrSlides = ActivePresentation.Slides _
.Range(Array("CostOfGoods", "SalesTotals", "Benefits", "Forecast"))
With sldCurrSlides
' Set properties common to all slides in this collection.
End With
Note You can also use the Array function as an argument to the Range method for a Shapes collection in order to return a collection of specified Shape objects as a ShapeRange object. For more information about working with shapes, see the next section, "Working with Shapes on Slides."