PowerPoint Macro: Getting the Texts of Title Slides

Sometimes, I have to get the texts of only the title slides.
So, I’ve made a PowerPoint VBA macro. This macro outputs it to the Immediate Window.

Macro Example

Sub GetTextsOfTitleSlides()

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld
    If .Layout = ppLayoutTitle Then
      Debug.Print _
        .SlideIndex & vbTab & _
        .Shapes.Title.TextFrame.TextRange
    End If
  End With
  Next sld

End Sub

Description

Variable Declaration

The 3rd line declares an object variable called sld that refers to a Slide object.

Sub...

  Dim sld As Slide

The Slide object represents an individual slide in a PowerPoint presentation.

obj-PowerPoint-Slide

Starts Looping

The 5th line starts to loop through all the elements (Slide) of a collection (Slides).

Sub...

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides

ActivePresentation property

  For Each sld In ActivePresentation...

The Application.ActivePresentation property returns an active Presentation object.

obj-PowerPoint-Application-ActivePresentation

And, the ActivePresentation property is a member of the PowerPoint.Global class, so we can omit the Application property.

obj-PowerPoint-ActivePresentation

Presentation.Slides property

  For Each sld In ActivePresentation.Slides

The Presentation.Slides property (ActivePresentation.Slides) returns a Slides object.

obj-PowerPoint-Presentation-Slides

The Slides object is a collection of all the Slide object in a presentation.

obj-PowerPoint-Slides

Starts the With statement

The 6th line starts the With…End With statement.
This statement tells PowerPoint that following any action applies to the Slide object.

Sub...

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld

Checks If the Slide is a Title Slide

The 7th line checks if the slide is a title slide.

Sub...

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld
    If .Layout = ppLayoutTitle Then

The Slide.Layout property returns or sets a PpSlideLayout constant that represents the slide layout.

obj-PowerPoint-Slide-Layout

Outputs the Text

If the slide is a title slide, the 8th-10th line outputs a slide index and text to the Immediate Window.

Sub...

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld
    If .Layout = ppLayoutTitle Then
      Debug.Print _
        .SlideIndex & vbTab & _
        .Shapes.Title.TextFrame.TextRange.Text

Slide.SlideIndex property

        .SlideIndex & vbTab & _

The Slide.SlideIndex property (sld.SlideIndex) returns the index number of the slide.

obj-PowerPoint-Slide-SlideIndex

And, the 10th line object model hierarchy is a little deep.

      .Shapes.Title.TextFrame.TextRange

Shapes.Title property

      .Shapes.Title...

The Shapes.Title property returns the Shape object that represents the slide title.

obj-PowerPoint-Shapes-Title

Shape.TextFrame property

      .Shapes.Title.TextFrame...

The Shape.TextFrame property (Title.TextFrame) returns the TextFrame object.

obj-PowerPoint-Shape-TextFrame

TextFrame.TextRange property

      .Shapes.Title.TextFrame.TextRange

The TextFrame.TextRange property returns the TextRange object.

obj-PowerPoint-TextFrame-TextTrange

As the Text property is a default property of the TextRange object, we can omit the Text property.

obj-PowerPoint-TextTrange-Text

So, the code
.Shapes.Title.TextFrame.TextRange is equal
.Shapes.Title.TextFrame.TextRange.Text .
It’s represents the title text.

Loops Back to Get the Next Slide

The 13th line loops back to get the next Slide object.

Sub...

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld
    If .Layout = ppLayoutTitle Then
      Debug.Print _
        .SlideIndex & vbTab & _
        .Shapes.Title.TextFrame.TextRange
    End If
  End With
  Next sld

After every slide is evaluated, this macro ends.

Sub GetTextsOfTitleSlides()

  Dim sld As Slide

  For Each sld In ActivePresentation.Slides
  With sld
    If .Layout = ppLayoutTitle Then
      Debug.Print _
        .SlideIndex & vbTab & _
        .Shapes.Title.TextFrame.TextRange
    End If
  End With
  Next sld

End Sub

Properties

http://www.relief.jp/itnote/archives/017675.php

Apply to

  • PowerPoint 2013
  • PowerPoint 2010
  • PowerPoint 2007