Header
PowerPoint tips, hints and tutorials that will change your presentations for ever!

INDEX

 

Jigsaws
Sounds
Video
Custom Shows
vba code
NaviSlides
Games for teachers
Bullets
Triggers
Security
Flash Cards
Multiple Instances
PowerPoint 2007
Mail Merge
Random events
Animation
Hyperlinks
Set spellcheck language


Home buttonTutorial buttonContact buttonProducts button


Delete Shapes with vba

If you have a little vba knowledge deleting certain shapes from a slide may seem fairly simple. Imagine we are looking for shapes with a 6 point line.

'READ ON THIS DOESN'T WORK

Sub die_line6()
Dim i As Integer
For i = 1 To ActivePresentation.Slides(1).Shapes.Count
If ActivePresentation.Slides(1).Shapes(i).Line.Weight = 6 Then _
ActivePresentation.Slides(1).Shapes(i).Delete
Next i
End Sub

Looks like it will loop through all the shapes and delete any with a 6 point line.

BUT it won't

Imagine there are 6  shapes and 4 & 5 have this line. When it gets to shape 4 it will be deleted. At this point though what was shape 5 will become shape 4. THe vba will jump this shape and test the old shape 6 (now shape 5). Next it will look for shape 6 which no longer exists! PowerPoint is now going to say whoa wheres six = CRASH!

Read that again and when you have your head round it look at:

'READ ON THIS DOESN'T WORK EITHER

Sub die_line6b()
Dim oshp As Shape
For Each oshp In ActivePresentation.Slides(1).Shapes
If oshp.Line.Weight = 6 Then _
oshp.Delete
Next oshp
End Sub

This looks promising and will usually work. Sometimes though it will seem to randomly fail to delete shapes which should go!

PowerPoint is smart enough to notice there are fewer shapes in the shape collection but can still miss out shapes if there are consequtive match shapes. In the example above only 1,2,3,4 & 6 are checked. As long as 5 is not a match you're good. If it is a match though ....

Proper Way

Loop BACKWARDS through the shapes this way if shape 5 is deleted shape 4 still exists!

Sub die()
Dim i As Integer
'reverse loop
For i = ActivePresentation.Slides(1).Shapes.Count To 1 Step -1
If ActivePresentation.Slides(1).Shapes(i).Line.Weight = 6 Then _
ActivePresentation.Slides(1).Shapes(i).Delete
Next i
End Sub

 
 

Back to the Index Page

POWERPOINT BLOG

Articles on your favourite sport

Free Microsoft PowerPoint Advice, help and tutorials, Template Links
This website is sponsored by Technology Trish Ltd
© Technology Trish 2007
Registered in England and Wales No.5780175
PowerPoint® is a registered trademark of the Microsoft Corporation