1
votes

I'm trying to delete all slides on a PowerPoint shape with VBA. What i'm trying to do is simply:

For Each sld In ActivePresentation.Slides
    For Each sh In sld.Shapes
       sh.Delete
    Next 
Next

But this does not delete all shapes as illustrated in the image below: (The grey arrow is a shape i'm adding before running the code below, but doesn't dissapear either)

Any idea on what i'm doing wrong?

enter image description here

5
Seem ok to me, under what event are you adding the shapes to begin with?Charlie Stuart
The shapes you see on the left slide is added manually. Before the above code is executed i added two arrow-lines to the slide from the code to see how it would behave. The grey line-arrow on the right slide is one of these arrows. The problem is present both if i add shapes from within the code or add them manually.Attaque

5 Answers

2
votes

That truly is interesting and I was able to re-create the problem. I also first tried to take a count and iterate through the count; however, the problem is that how many shapes are on there refreshes real-time. What I mean is if it counted the shapes at one point in time as 2 shapes, then when a shape is deleted there are only 1 shape on there. Therefore your best bet is to loop through the count of shapes deleting the first shape each time as such.

For Each sld In ActivePresentation.Slides

    TotalShapes = sld.Shapes.Count

    For i = TotalShapes to 1 step -1 

        sld.Shapes(i).Delete

    Next

Next

Thanks, Sean W.

1
votes
typo: missing 0 in > 0

While ActivePresentation.Slides.Count > 0
      While ActivePresentation.Slides(1).Shapes.Count **> 0**
            ActivePresentation.Slides(1).Shapes(1).Delete
      Wend
      ActivePresentation.Slides(1).Delete
Wend
1
votes

Or you can just say:

sld.Shapes.Range.Delete
0
votes
For Each sld In ActivePresentation.Slides
    For x = sld.shapes.count to 1 step -1
       sld.shapes(x).Delete
    Next 
Next
0
votes

I think, the easiest way is:

while ActivePresentation.Slides.count>0
  while ActivePresentation.Slides(1).shapes.count>
    ActivePresentation.Slides(1).shapes(1).delete
  wend
  ActivePresentation.Slides(1).delete
wend