Let us look at some ways to get a
reference to the Title shape. First and foremost it is always prudent to
check if the slide has a title shape present or not. This saves us the
trouble of looking for something that might not be there. The
HasTitle property of the Shapes collection on the slide returns a
TRUE if the slide has a title.
Thanks to Kedamano for
providing the third method
Top
'
--------------------------------------------------------------------------------
' Copyright ©1999-2007, Shyam Pillai, All Rights Reserved.
'
--------------------------------------------------------------------------------
' You are free to use this code within your own applications, add-ins,
' documents etc but you are expressly forbidden from selling or
' otherwise distributing this source code without prior consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
'
--------------------------------------------------------------------------------
' Method 1: Looping thru the
placeholders collection
' The title shape is a placeholder
which is of the type ppPlaceholderCenterTitle
' or ppPlaceholderTitle
these two types never occur on the same slide.
Sub LoopPlaceHolderCollection()
Dim oTitle As Shape
Dim oShp As Shape
With ActivePresentation.Slides(1).Shapes
If .HasTitle Then
For Each oShp In .Placeholders
If oShp.PlaceholderFormat.Type =
ppPlaceholderCenterTitle Or _
oShp.PlaceholderFormat.Type = ppPlaceholderTitle Then
Set oTitle = oShp
End If
Next oShp
End If
If Not oTitle Is Nothing Then
MsgBox "Title of the slide is: " & _
oTitle.TextFrame.TextRange.Text, _
vbInformation
Else
MsgBox "This slide has no Title shape
present", _
vbExclamation
End If
End With
End Sub
' Method 2: Using the Title
property
' This exploits the fact that if a
title shape is present on the slide the Title
' property will return the reference
to the title shape.
Sub UseTitleProperty()
Dim oTitle As Shape
With ActivePresentation.Slides(1).Shapes
If .HasTitle Then
Set oTitle = .Title
MsgBox "Title of the slide is: " & _
oTitle.TextFrame.TextRange.Text, _
vbInformation
Else
MsgBox "This slide has no Title shape present", _
vbExclamation
End If
End With
End Sub
' Method 3: Using the
PlaceHolders collection
' This exploits the fact that if a
title shape is present on the slide it will always
' be the first shape in the
placeholders collection.
Sub UsePlaceHolders()
Dim oTitle As Shape
With ActivePresentation.Slides(1).Shapes
If .HasTitle Then
Set oTitle = .Placeholders(1)
MsgBox "Title of the slide is: " & _
oTitle.TextFrame.TextRange.Text, _
vbInformation
Else
MsgBox "This slide has no Title shape present", _
vbExclamation
End If
End With
End Sub
Top
|