Deleting shapes

F

Fuzzhead

I have the following macro that allows the user to draw vertical lines in the
document. How can I remove all of them later from the document at one time
without removing other shapes in the document?

Sub NewLine()
Dim lineNew As Shape
Dim i, j
On Error GoTo EndThis
i = Selection.Information(wdVerticalPositionRelativeToPage)
j = InchesToPoints(InputBox("BAR LENGTH {In Inches}:"))
Set lineNew = ActiveDocument.Shapes.AddLine(575, i, 575, j + i)
EndThis:
End Sub
 
J

Jay Freedman

At the top of the module, before any procedure, declare a variable to
serve as a "counter" or "index" of your lines:

Dim idx As Integer

In the macro that creates the lines, assign the .Name property of each
line you create -- including the index serves to make all the names
unique:

Set lineNew = ActiveDocument.Shapes.AddLine(575, i, 575, j + i)
lineNew.Name = "line" & idx
idx = idx + 1

Then you can distinguish your lines from other shapes because they'll
all have "line" in their names:

Sub DeleteLine()
Dim shp As Shape
For Each shp In ActiveDocument.Shapes
If InStr(shp.Name, "line") > 0 Then
shp.Delete
End If
Next
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

How about:

Sub ScratchMacro()
Dim i As Long
For i = ActiveDocument.Range.ShapeRange.Count To 1 Step -1
If ActiveDocument.Range.ShapeRange(i).Type = msoLine Then
ActiveDocument.Range.ShapeRange(i).Delete
End If
Next
End Sub
 
F

Fuzzhead

That worked. Thanks again.



Greg Maxey said:
How about:

Sub ScratchMacro()
Dim i As Long
For i = ActiveDocument.Range.ShapeRange.Count To 1 Step -1
If ActiveDocument.Range.ShapeRange(i).Type = msoLine Then
ActiveDocument.Range.ShapeRange(i).Delete
End If
Next
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
J

Jay Freedman

Hi Greg,

Yeah, I hadn't thought about that. Your solution is better if you don't have
control over creating the lines and assigning names to them, but mine is
better if the user might have used the Line tool to draw other lines in the
document. It's a tradeoff.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

Jay,

I agree. After I posted, I tried my own solution on a document I have that
has about 700 shapes (365 ballons and 365 lines). I ran my code and to my
surprise, there where still 3 lines present. I don't know why as all 365
lines where drawn with the line tool, but those 3 lines where of type
msoAutoshape vice msoLine.

Yours is better period.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Finding shapes 12
Draw Table 4
AddPicturesToShapes 0
Shape size 4
Drawing lines 2
More efficient macro 5
Textbox help 2
Variable question 2

Top