PDA

View Full Version : Code to delete slides IF they contain pictures of a certain name (e.g. "Picture 2")?



ajjava
03-15-2019, 09:26 AM
I have a deck of slides, each containing various objects. I would like to delete any slide that does NOT contain a picture object name EITHER "Picture 2" or "Picture 3".
It seems like this should be easy, but I'm getting caught up on the For Next/With in the case that the slide is deleted.
THanks!!!

John Wilson
03-16-2019, 04:40 AM
Something like this maybe:


Function hasPicture(oshp As Shape) As Boolean
If oshp.Type = msoPicture Then
hasPicture = True
Exit Function
End If
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.ContainedType = msoPicture Then
hasPicture = True
Exit Function
End If
End If
End Function




Function hasTargetPic(osld As Slide) As Boolean
Dim oshp As Shape
For Each oshp In osld.Shapes
If hasPicture(oshp) Then
If oshp.Name = "Picture 2" Or oshp.Name = "Picture 3" Then
hasTargetPic = True
Exit Function
End If
End If
Next oshp
End Function


Sub deleter()
Dim S As Long
For S = ActivePresentation.Slides.Count To 1 Step -1
If Not hasTargetPic(ActivePresentation.Slides(S)) Then _
ActivePresentation.Slides(S).Delete
Next S
End Sub