How to Isolate Shapes in a Layer

S

SteveM

I have some shapes that I have assigned to the Layer "LayerName". I
then cycled through the shapes using the code construct (I think) of
the http://visio.mvps.org/VBA.htm site.

Dim vShape as Shape
For Each vShape In ActivePage.Layers("LayerName").Page.Shapes
Do some stuff
Next

Which works fine until I add a CommandButton control to the page. The
For Each loop then includes the Button when it cycles even though the
Button has not been assigned to the LayerName Layer.

Can anyone set me straight on restricting loop access to only the
shapes in the named layer?

Thanks Much,

Steve
 
J

JuneTheSecond

Hi, Steve.

Please try next code.
the page in your ... .Layers("LayerName").Page means
the page that includes layer "LayerName", but not
the page that is included in the layer.


Dim vShape As Shape
Dim I As Integer

For Each vShape In ActivePage.Shapes
If vShape.CellExists("LayerMember", False) Then
For I = 1 To vShape.RowsCellCount(visSectionObject,
visRowLayerMem)
If vShape.Layer(I).Name = "LayerName" Then
Debug.Print vShape.Name
End If
Next
End If
Next
 
S

SteveM

Hi, Steve.

Please try next code.
the page in your ... .Layers("LayerName").Page means
the page that includes layer "LayerName", but not
the page that is included in the layer.

Dim vShape As Shape
Dim I As Integer

For Each vShape In ActivePage.Shapes
If vShape.CellExists("LayerMember", False) Then
For I = 1 To vShape.RowsCellCount(visSectionObject,
visRowLayerMem)
If vShape.Layer(I).Name = "LayerName" Then
Debug.Print vShape.Name
End If
Next
End If
Next

J2,

Thanks for the code. It works.Below my signature are two examples
from the Visio MVP webpage. I used the construct of the first one
without the outer loop because I only had one page and one layer.
That one did not work. It included the Control. The second example
does work, but I can't understand why it has to be so complicated.
I'm a mathematical modeler not a programmer. So forgive me if this
seems naïve. It seems to me that the layer is a container of
different OLE types. I'm surprised that you can't select the objects
by type, e.g. Visio Shapes, Visio Data Graphics, etc. and then operate
on those. I've probably had more trouble selecting the proper set of
objects with which I want to operate than anything else in working
with Visio. I studied quantum mechanics in an earlier life. And
Visio is like that. You can learn it. But it sure ain't intuitive.

Thanks,

SteveM

*** Does Not Work ***

Public Sub LayersContent()
Dim PagObj As Visio.Page
Dim layersObj As Visio.Layers, layerObj As Visio.Layer
Dim shpsObj As Visio.Shapes, shpObj As Visio.Shape

For Each PagObj In ActiveDocument.Pages
Set layersObj = PagObj.Layers
For Each layerObj In layersObj
Set shpsObj = layerObj.Page.Shapes
For Each shpObj In shpsObj
Debug.Print layerObj.Name; " "; shpObj.Name
Next
Next
Next
End Sub

*** Works ***

Public Sub LayersContent2()
Dim PagObj As Visio.Page
Dim layersObj As Visio.Layers, layerObj As Visio.Layer
Dim shpsObj As Visio.Shapes, shpObj As Visio.Shape
Dim I As Long, N As Long

For Each PagObj In ActiveDocument.Pages
For Each shpObj In PagObj.Shapes
N = shpObj.LayerCount
If N > 0 Then
For I = 1 To N
Set layerObj = shpObj.Layer(I)
Debug.Print PagObj.Name; " "; shpObj.Name; " ";layerObj.Name
Next I
End If
Next ShpObj
Next PagObj
End Sub
 
S

SteveM

J2,

Thanks for the code. It works.Below my signature are two examples
from the Visio MVP webpage. I used the construct of the first one
without the outer loop because I only had one page and one layer.
That one did not work. It included the Control. The second example
does work, but I can't understand why it has to be so complicated.
I'm a mathematical modeler not a programmer. So forgive me if this
seems naïve. It seems to me that the layer is a container of
different OLE types. I'm surprised that you can't select the objects
by type, e.g. Visio Shapes, Visio Data Graphics, etc. and then operate
on those. I've probably had more trouble selecting the proper set of
objects with which I want to operate than anything else in working
with Visio. I studied quantum mechanics in an earlier life. And
Visio is like that. You can learn it. But it sure ain't intuitive.

Thanks,

SteveM

*** Does Not Work ***

Public Sub LayersContent()
Dim PagObj As Visio.Page
Dim layersObj As Visio.Layers, layerObj As Visio.Layer
Dim shpsObj As Visio.Shapes, shpObj As Visio.Shape

For Each PagObj In ActiveDocument.Pages
Set layersObj = PagObj.Layers
For Each layerObj In layersObj
Set shpsObj = layerObj.Page.Shapes
For Each shpObj In shpsObj
Debug.Print layerObj.Name; " "; shpObj.Name
Next
Next
Next
End Sub

*** Works ***

Public Sub LayersContent2()
Dim PagObj As Visio.Page
Dim layersObj As Visio.Layers, layerObj As Visio.Layer
Dim shpsObj As Visio.Shapes, shpObj As Visio.Shape
Dim I As Long, N As Long

For Each PagObj In ActiveDocument.Pages
For Each shpObj In PagObj.Shapes
N = shpObj.LayerCount
If N > 0 Then
For I = 1 To N
Set layerObj = shpObj.Layer(I)
Debug.Print PagObj.Name; " "; shpObj.Name; " ";layerObj.Name
Next I
End If
Next ShpObj
Next PagObj
End Sub


J2,

I also did what I guess I should have done at the beginning. Generate
the proper selection using the marco recorder. After doing that and
then cutting out the extraneous recorder code, I got this which also
works:

Dim vsoSelection1 As Visio.Selection
Dim vShape As Shape
Set vsoSelection1 =
Application.ActiveWindow.Page.CreateSelection(visSelTypeByLayer,
visSelModeSkipSuper, "LayerName")
For Each vShape In vsoSelection1
Do Something
Next

SteveM
 
J

JuneTheSecond

To me Visio layer is not the layer that
can be seen in cad system in the world.
Visio layer seems to me that was added
after whole Visio structure is completed.
Visio layer seems to me just a property
of shape, but not the property of page.
Visio layer seems to me not well structured.
 
S

SteveM

To me Visio layer is not the layer that
can be seen in cad system in the world.
Visio layer seems to me that was added
after whole Visio structure is completed.
Visio layer seems to me just a property
of shape, but not the property of page.
Visio layer seems to me not well structured.

J2,

Back to this topic. I was experimenting and come up with this compact
solution:

Note: "Group1" is the Name of the Group containing the Shapes

Public Sub CycleThruGroup()
Dim grpShape
Dim vShape As Shape

Set grpShape = ActivePage.Shapes("Group1")
For Each vShape In grpShape.Shapes
vShape.DoSomthing
Next

End Sub

SteveM
 

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