Working with the Slides Collection

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.

You primarily use the Slides collection to add new slides to a presentation or to access a specific slide within a presentation. You use the Slides collection's Add method to add a new slide to a collection. You use arguments of the Add method to specify the location of the slide in the Slides collection and to specify the slide's layout. The following example shows how you would add a new blank slide to the end of the current Slides collection:

Dim sldNewSlide            As PowerPoint.Slide
Dim lngLastSlideAdded      As Long

With ActivePresentation
   Set sldNewSlide = .Slides.Add(.Slides.Count + 1, ppLayoutBlank)
   With sldNewSlide
      ' Add code to set properties of the slide here.
      lngLastSlideAdded = .SlideID
   End With
End With

You can add existing slides, or data that can be converted to slides, to a presentation by using the Slides collection's InsertFromFile method. For example, you could create a new presentation that used the opening and closing slides from a company presentation template and then used a Word outline to create the slides that make up the body of the presentation:

Dim ppApp      As New PowerPoint.Application
Dim prsPres    As PowerPoint.Presentation

With ppApp
   Set prsPres = .Presentations.Add
   With prsPres
      .ApplyTemplate "c:\corp\corpPresentations.pot"
      .Slides.InsertFromFile "c:\PPTOutline.doc", 1
   End With
End With

To locate a slide within the collection, you use the Slides collection's FindBySlideID method. Each slide in a PowerPoint presentation has a SlideID property that is a Long Integer value that uniquely identifies the slide regardless of its location in the Slides collection. When you add to or delete slides from a collection, a slide's index value might change, but its SlideID property will always be the same. The first code sample in this section illustrates how to save the SlideID property to a variable so that it might be used again to locate the slide. The following sample shows how to locate a slide by using the Long Integer value representing the SlideID property:

Function FindSlide(lngID As Long) As PowerPoint.Slide
   ' This procedure returns the slide whose SlideID property value 
   ' matches lngID. If no match is found, the return value of the 
   ' procedure is = Nothing.
   On Error Resume Next
   Set FindSlide = ActivePresentation.Slides.FindBySlideID(lngID)
End Function

See Also

Working with PowerPoint Slides | Working with Slide Objects