- Remove From My Forums
-
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 SubThe 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?
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
Tuesday, August 30, 2011 8:45 AM
-
Marked as answer by
title | keywords | f1_keywords | ms.prod | api_name | ms.assetid | ms.date | ms.localizationpriority |
---|---|---|---|---|---|---|---|
Shape.Delete method (Word) |
vbawd10.chm161480715 |
vbawd10.chm161480715 |
word |
Word.Shape.Delete |
7125f94f-fe82-dacf-b407-9e2bb374dbc4 |
06/08/2017 |
medium |
Shape.Delete method (Word)
Deletes the specified shape node.
Syntax
expression.Delete (Index)
expression Required. A variable that represents a Shape object.
Parameters
Name | Required/Optional | Data type | Description |
---|---|---|---|
Index | Required | Long | The number of the shape node to delete. |
See also
Shape Object
[!includeSupport and feedback]
I have a macro that generates a case label for a list of different products. Some of the products are sterile and require a drawing of a circle to be placed on the label as a location for a radiation indicator dot. There is also a text box inside the circle that labels the circle as the location for the dot. I tried to do this by inserting an autoshape of a circle and making it a bookmark and then using the code:
ThisDocument.Bookmarks("GammaDot").Range.Delete
to delete the circle on all the parts that aren’t sterile. This code works to delete the text from the text box inside the circle, but the circle itself doesn’t get deleted.
It also seems that the text box itself isn’t getting deleted, just the text inside the box. Does the bookmarks.Delete command not work on actual obects? and if it doesn’t, how would I go about deleting the circle and text box?
Thank you
asked Dec 4, 2010 at 21:50
I suspect you will have to either delete the shapes by name or in a loop. The bookmark parent returns the document, not the textbox. This will delete both the textbox and the circle:
For i = ThisDocument.Shapes.Count To 1 Step -1
''Debug.Print ThisDocument.Shapes(1).Name
ThisDocument.Shapes(i).Delete
Next
answered Dec 5, 2010 at 12:41
FionnualaFionnuala
90.1k7 gold badges110 silver badges148 bronze badges
0
You can get hold of the shapes belonging to a bookmark using the ShapeRange property of the bookmark’s Range, and the shape’s text using its TextFrame:
Dim bkmk As Bookmark
Set bkmk = ActiveDocument.Bookmarks("circle")
Dim shp As Shape
Set shp = bkmk.Range.ShapeRange.Item(1)
Debug.Print shp.TextFrame.TextRange.Text
shp.Delete
Deleting the shape will also remove the contained text.
answered Dec 6, 2010 at 14:45
Tom JuergensTom Juergens
4,4823 gold badges34 silver badges32 bronze badges
You can delete everything except text and its formatting by running following code in vba editor:
Sub DeleteAllExceptText()
Dim i As Integer
With ActiveDocument
For i = .Tables.Count To 1 Step -1
.Tables(i).Delete
Next i
End With
Dim j As Integer
With ActiveDocument
For j = .Shapes.Count To 1 Step -1
.Shapes(j).Delete
Next j
Dim k As Integer
With ActiveDocument
For k = .InlineShapes.Count To 1 Step -1
.InlineShapes(k).Delete
Next k
End Sub
answered Aug 2, 2017 at 9:42
How to remove shape in Ms Word using VBA Macro
In Microsoft word, it provides the approach to write VBA Function known as Macro to perform automatically tasks in Ms Word. We can also implement VBA class objects with VBA’s function for the automatically tasks. First, write your own function to remove all shapes which contain in Word File. VBA contains a Class Object called Shape and Shapes to deal with all shapes. We can use Shape Object to delete, select or duplicate the shape.
Code Sample to delete all shapes
Here is complete code example to remove all shapes. This example uses Shapeobject in VBA:
SubRemoveShapes()
Dim oShape AsShape
Do
For EachoShape In ActiveDocument.Shapes
‘ Delete the shape
oShape.Delete
NextoShape
Loop UntilActiveDocument.Shapes.Count = 0
Set oShape = Nothing
End Sub
How to test this Code
1. Open Ms Word
2. Open VB Editor or Alt+F11
3. Create new module
4. Copy and paste the Code
5. Go back to you word and go to Macro Dialog then select the Macro Name RemoveShape and Click on Run
That’s all on how to remove all shapes in Ms Word.
- Forum
- VBA Code & Other Help
- Word Help
- Find, Select & Delete Shapes
-
02-17-2018, 07:32 AM
#1
Find, Select & Delete Shapes
Hi,
I have a large Word document with a lot of WordArt shapes. I would like to find & delete them using a macro.
Perhaps with a dialog box with OK and Cancel so I can decide what to do.In Word the WordArt shapes have names like «WordArt 12»
It’s been a long long time I’ve used VBA. I think it must be possible to use a concatenated string:
dim myWordArt = "WordArt" & number
and then somehow find, select & delete the WordArt shapes.
Anyone?
Last edited by Paul_Hossler; 02-17-2018 at 10:23 AM.
Reason: replaced vba tags with CODE tags
-
02-17-2018, 09:53 AM
#2
Provided that they are anchored to the main text storyrange, something like this may do. Otherwise you will have to loop through some the other storyranges:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 2/17/2018 Dim oShp As Shape Dim lngIndex As Long For lngIndex = ActiveDocument.Shapes.Count To 1 Step -1 Set oShp = ActiveDocument.Shapes(lngIndex) oShp.Select Application.ScreenRefresh If MsgBox("Do you want to delete this shape?", vbYesNo, "DELETE") = vbYes Then oShp.Delete End If Next lngIndex lbl_Exit: Exit Sub End Sub
-
02-17-2018, 03:53 PM
#3
Since WordArt objects are ordinarily inserted in-line, you may need something closer to:
Sub Demo() Dim i As Long With ActiveDocument For i = .InlineShapes.Count To 1 Step -1 With .InlineShapes(i) If .Type = wdInlineShapePicture Then .Select If MsgBox("Delete this shape?", vbYesNo, "DELETE") = vbYes Then .Delete End If End With Next End With End Sub
Cheers
Paul Edstein
[Fmr MS MVP — Word]
-
02-18-2018, 04:15 AM
#4
Thanks guys but how can I select only those shapes that have a name like «WordArt 842» (and show the name for confirmation of delete)?
Something like this :Sub Demo() Dim i As Long Dim myString as String With ActiveDocument For i = .InlineShapes.Count To 1 Step -1 With .InlineShapes(i) If .Type = wdInlineShapePicture Then myString = substring(wdInlineShapePicture.name,1,7) if myString = "WordArt" Then .Select If MsgBox("Delete " & wdInlineShapePicture.name & " ?", vbYesNo, "DELETE") = vbYes Then .Delete End If End If End With Next End With End Sub
Last edited by arjopost; 02-18-2018 at 04:21 AM.
Reason: still typing code
-
02-18-2018, 04:34 AM
#5
InlineShapes don’t have a name property. If your WordArt objects have names, that suggests someone has converted them to a wrapped format. In that case you might use:
Sub Demo() Dim i As Long With ActiveDocument For i = .Shapes.Count To 1 Step -1 With .Shapes(i) If .Type = msoTextEffect Then If .Name Like "WordArt*" Then .Select If MsgBox("Delete this shape? " & .Name, vbYesNo, "DELETE") = vbYes Then .Delete End If End If End With Next End With End Sub
Cheers
Paul Edstein
[Fmr MS MVP — Word]
-
02-18-2018, 04:57 AM
#6
Thanks Paul this it works just fine. I have another issue that is in line with this solution; I have my name in several textareas in the same document. So not in a WordArt object but in a textarea. They have names like «Tekstvak 622». Now I would like to search for my name «Arjo» that is inside the content of the textarea, and then select & delete the textareas it is in. Something with .Parent maybe?
-
02-18-2018, 06:44 AM
#7
I finally did some research myself
Sub FindDeleteTextBox() dim i as Long dim sTemp as String With ActiveDocument For i = .Shapes.Count To 1 Step -1 With .Shapes(i) If .Type = msoTextBox Then .Select Selection.ShapeRange.TextFrame.TextRange.Select sTemp = Selection.Text If sTemp Like "*Arjo*" Then If MsgBox("Delete this textbox? " & .Name, vbYesNo, "DELETE") = vbYes Then .Delete End If End If End With Next End With End Sub
Thanks for all the help! My problems are solved so this thread can be closed.
Last edited by arjopost; 02-18-2018 at 07:01 AM.
-
02-18-2018, 07:42 AM
#8
Paul,
Since Word 2010, WordArt is inserted as floating text. That is why I suggested to loop through the shapes initially.
-
02-18-2018, 12:42 PM
#9
Fair enough — I’m still using Word 2010.
Cheers
Paul Edstein
[Fmr MS MVP — Word]
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] code is On
- HTML code is Off
Forum Rules