Working with Slide Objects

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

By default, Microsoft® 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:

  • By using an index value representing the location of the slide in the Slides collection.
  • By using the slide's name.
  • By using the slide's SlideID property with the Slides collection's FindBySlideID method.
  • By using the SlideIndex property of the SlideRange object from the PowerPoint Selection object to return the currently selected slide; however, using the SlideIndex property in this manner might return an error, if more than one slide is selected.

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.

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 Microsoft® Visual Basic® for Applications (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.

See Also

Working with PowerPoint Slides | Working with the Slides Collection | Working with Shapes on Slides