PDA

View Full Version : [SOLVED:] Select most items on a slide and delete them



stephanie.ma
04-11-2022, 01:50 PM
I want to

1. select a particular slide from the active presentation
A. assuming something like the following for slide number 8

ActivePresentation.Slides(8).Select
2. select/Delete most of the shapes that are on that slide.
A. if the shape has the word "placeholder" it will stay on the slide
B. a slide could have 2 items or 5.
C. names of pictures will vary
D. they could have Rectangle 5, Slide Number Placeholder1, and Picture 3 as names
3. then I will insert a photo from a file to replace the deleted one(s)....

my focus for this question is selecting the items and getting them deleted.

Paul_Hossler
04-11-2022, 02:46 PM
https://docs.microsoft.com/en-us/office/vba/api/office.msoshapetype

https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppplaceholdertype

i wouldn't trust the .Name

I think it'd be safer to use Shape.Type and Shape.PlaceHolderFormat.Type



Option Explicit


Sub test()
Dim oPres As Presentation
Dim oSlide As Slide
Dim oShape As Shape

Set oPres = ActivePresentation

For Each oSlide In oPres.Slides
For Each oShape In oSlide.Shapes
With oShape
If .Type = msoPlaceholder Then
If .PlaceholderFormat.Type = ppPlaceholderCenterTitle Then
MsgBox "Title"
End If

ElseIf .Type = msoPicture Then
MsgBox "Picture"

Else
MsgBox "Somethin Else"
End If
End With
Next
Next


End Sub

John Wilson
04-11-2022, 10:36 PM
Maybe this would work


Sub killer()
Dim osld As Slide, L As Long
Set osld = ActivePresentation.Slides(1)' change number
For L = osld.Shapes.Count To 1 Step -1
If osld.Shapes(L).Type <> msoPlaceholder Then
osld.Shapes(L).Delete
End If
Next
End Sub

In PPT always try to avoid select shapes / slides in code unless it is really necessary BTW. Work on a copy just in case.

stephanie.ma
04-12-2022, 06:39 AM
John's code worked perfectly. I am going to use Paul's code to add additional code to insert the new photo after the slides are successfully deleted.
thank you for the easy to understand explanations. you guys are amazing:beerchug:

Paul_Hossler
04-12-2022, 08:25 AM
Be advised that if you have Grouped Shapes, then they might require some special checks