Do something to every shape slide or file
Here are simple examples of code that illustrate how you can perform some operation on:
- Every shape on a slide
- Every slide in a presentation
- Every presentation in a folder
- Every text box on every slide in a presentation
Note: since some of these routines call one another, copy and paste them all into a single module.
Sub EveryTextBoxOnSlide()
' Performs some operation on every shape that contains text on every slide
' (doesn't affect charts, tables, etc)
Dim oSh As Shape
Dim oSl As Slide
On Error GoTo ErrorHandler
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
With oSh
If .HasTextFrame Then
If .TextFrame.HasText Then
' If font size is mixed, don't touch the font size
If .TextFrame.TextRange.Font.Size > 0 Then
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size + 2
End If
End If
End If
End With
Next ' shape
Next ' slide
NormalExit:
Exit Sub
ErrorHandler:
Resume Next
End Sub
Sub EverySlideInPresentation()
' Performs some operation on every slide in the currently active presentation
Dim oSl As Slide
For Each oSl In ActivePresentation.Slides
' for example, show its name and index number:
Debug.Print oSl.Name & vbTab & oSl.SlideIndex
' or do something with every shape on the slide:
Call EveryShapeOnSlide(oSl)
Next oSl
End Sub
Sub EveryPresentationInFolder()
' Performs some operation on every presentation file in a folder
Dim sFolder As String ' Full path to folder we'll examine
Dim sFileSpec As String ' Filespec, e.g. *.PPT
Dim sFileName As String ' Name of a file in the folder
Dim oPres As Presentation
' Edit this:
sFolder = "C:\Files\" ' must end with a \ character
sFileSpec = "*.PPT"
' Get the first filename that matches the spec:
sFileName = Dir$(sFolder & sFileSpec)
While sFileName <> ""
' do something with the presentation ...
' Open it
Set oPres = Presentations.Open(sFolder & sFileName, msoFalse)
' Display the number of slides in it
Debug.Print oPres.Slides.Count
' Or you could do something to every slide in the presentation:
Call EverySlideInPresentation
' close the presentation
oPres.Close
' release the reference
Set oPres = Nothing
' Once done, see if there's another presentation that meets our spec
' then around the loop again
sFileName = Dir()
Wend
End Sub
Sub EveryShapeOnSlide(oSl as Slide)
' Performs some operation on every shape on a slide
Dim oSh As Shape
On Error GoTo ErrorHandler
For Each oSh In oSl.Shapes
' Show the name of the shape:
Debug.Print oSh.Name
' or whatever else you want to do
' for example, ungroup/regroup certain types of shapes:
Select Case oSh.Type
Case Is = msoEmbeddedOLEObject, msoLinkedOLEObject, msoPicture
' Attempting to ungroup a bitmap image causes an error
' but no harm is done; we'll ignore it.
On Error Resume Next
oSh.Ungroup.Group
On Error GoTo ErrorHandler
Case Else
' ignore other shape types
End Select
Next oSh
NormalExit:
Exit Sub
ErrorHandler:
Resume Next
End Sub
Note: In practice, it's better to collect the filenames first (ie, in an array) then process the filenames from the array
See Do something to every file in a folder
See How do I use VBA code in PowerPoint? to learn how to use this example code.