for each !?

M

Murat

Hello,

I have a page containing rectange and mid-arrow shapes. I want to delete
just mid-arrow shapes. I write a code:

Dim PageShapes As Visio.Shapes
Dim shp As Visio.Shape
Dim BittiMi As Boolean

Set PageShapes = Visio.ActiveDocument.Pages.Item("Page1").Shapes

While BittiMi = False

BittiMi = True

For Each shp In PageShapes
If shp.Master.Name = "Mid-arrow" Then
shp.Delete
End If

Next shp

For Each shp In PageShapes
If shp.Master.Name = "Mid-arrow" Then
BittiMi = False
Exit For
End If
Next shp
Wend


This code deletes some mid-arrow shapes but it generates a error: "Run-time
error '91': Object variable or With block variable not set".

for
[If shp.Master.Name = "Mid-arrow" Then ] row in that code.

Could you tell me how to solve that?

Thanks all..
 
J

JuneTheSecond

Sometimes a progam causes error, if I delete the key object in loop. My
recommendation is to select found shape, and delete the selection after the
end of the loop. But it is not always true, I have experienced a case that
has no problem in Visio2003.

Foe example next program has no error in Visio2003, but stop in error in
Visio2000.

Sub DeleteShapes()
Dim myShape As Shape
For Each myShape In Visio.ActivePage.Shapes
If myShape.Data1 = "ABCDE" Then
myShape.Delete
End If
Next
End Sub

So, I changed the code as,,,
Sub DeleteShapes1()
Dim myShape As Shape
For Each myShape In Visio.ActivePage.Shapes
If myShape.Data1 = "ABCDE" Then
ActiveWindow.Select myShape, visSelect
End If
Next
ActiveWindow.Selection.Delete
End Sub
 
J

John Marshall, MVP

I do not use the For Each for deletion, but the older method of For
i=maxshapes to 1 step -1

The problem with using For i=1 to maxhapes is that each time you delete a
shape, the collection gets adjusted. So after deleting item n, you are
actually pointing item n+1 and the next deletion will be for item n+2. Going
in reverse order means the shapes to be processed are not affected.

I'm not sure that this is the case with FOR EACH, but you may want display
the values as you loop through the collection.

John... Visio MVP

Need stencils or ideas? http://www.mvps.org/visio/3rdparty.htm
Need VBA examples? http://www.mvps.org/visio/VBA.htm
Common Visio Questions http://www.mvps.org/visio/common_questions.htm
Visio Wishlist http://www.mvps.org/visio/wish_list.htm
 
M

Murat

For
i=maxshapes to 1 step -1

Thank you, it really works good.



John Marshall said:
I do not use the For Each for deletion, but the older method of For
i=maxshapes to 1 step -1

The problem with using For i=1 to maxhapes is that each time you delete a
shape, the collection gets adjusted. So after deleting item n, you are
actually pointing item n+1 and the next deletion will be for item n+2. Going
in reverse order means the shapes to be processed are not affected.

I'm not sure that this is the case with FOR EACH, but you may want display
the values as you loop through the collection.

John... Visio MVP

Need stencils or ideas? http://www.mvps.org/visio/3rdparty.htm
Need VBA examples? http://www.mvps.org/visio/VBA.htm
Common Visio Questions http://www.mvps.org/visio/common_questions.htm
Visio Wishlist http://www.mvps.org/visio/wish_list.htm

Murat said:
Hello,

I have a page containing rectange and mid-arrow shapes. I want to delete
just mid-arrow shapes. I write a code:

Dim PageShapes As Visio.Shapes
Dim shp As Visio.Shape
Dim BittiMi As Boolean

Set PageShapes = Visio.ActiveDocument.Pages.Item("Page1").Shapes

While BittiMi = False

BittiMi = True

For Each shp In PageShapes
If shp.Master.Name = "Mid-arrow" Then
shp.Delete
End If

Next shp

For Each shp In PageShapes
If shp.Master.Name = "Mid-arrow" Then
BittiMi = False
Exit For
End If
Next shp
Wend


This code deletes some mid-arrow shapes but it generates a error:
"Run-time
error '91': Object variable or With block variable not set".

for
[If shp.Master.Name = "Mid-arrow" Then ] row in that code.

Could you tell me how to solve that?

Thanks all..
 

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

Top