none
Word VBA to delete all shapes RRS feed

  • Question

  • I'm trying to use the following code to delete all shapes in a Word document (2007):

    Sub DeleteAllShapes()
    Dim shp As Word.Shape
    For Each shp In ActiveDocument.Shapes
        shp.Delete
    Next
    End Sub

    The problem is that it doesn't delete all the shapes on the first pass; in fact it deletes half of them. On the next pass it deletes another 50%, and so on. Obviously it's skipping a shape each time one gets deleted.

    Is there a way to use the collection so that all the shapes get deleted?

     

     

    Friday, August 26, 2011 2:01 PM

Answers

  •  

    You can't delete the collection, but you can step backwards through it....

    For i = ActiveDocument.Shapes.Count To 1 Step -1
    ActiveDocument.Shapes(i).Delete
    Next i


    HTH, Bernie
    • Marked as answer by jamesp1 Tuesday, August 30, 2011 8:45 AM
    Friday, August 26, 2011 6:57 PM

All replies

  •  

    You can't delete the collection, but you can step backwards through it....

    For i = ActiveDocument.Shapes.Count To 1 Step -1
    ActiveDocument.Shapes(i).Delete
    Next i


    HTH, Bernie
    • Marked as answer by jamesp1 Tuesday, August 30, 2011 8:45 AM
    Friday, August 26, 2011 6:57 PM
  • True - you can't delete the collection, but you can Select it, and delete the selection ...
     
        Application.ScreenUpdating = False
        ActiveDocument.Shapes.SelectAll
        Selection.Delete
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Sunday, August 28, 2011 7:41 PM
  • Thanks Bernie, that worked perfectly
    Tuesday, August 30, 2011 8:46 AM