Visio Layer Selection Problem for Shapes inside Group

Started by metuemre, December 07, 2017, 12:02:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

metuemre

Hi all,

I'm having a strange issue with selection of Layers. Let's say I have two shapes inside of a group and only one shape is assigned to a layer. The problem is that I can't access that sub shape with CreateSelection method. I attached a sample file and below is the sample code that I tried. What am I doing wrong?

Sub Layer_Check()
Dim mainshp As Visio.Shape
Dim sel As Visio.Selection
Dim vsoLayer As Visio.Layer

Set mainshp = ActivePage.Shapes("GroupShape")
Set vsoLayer = ActivePage.Layers("SubLayer")
Set sel = mainshp.CreateSelection(visSelTypeByLayer, visSelModeOnlySub, vsoLayer)

Debug.Print sel.Count

End Sub

metuemre

I think I found the answer on this old topic http://visguy.com/vgforum/index.php?topic=3955.msg15450#msg15450

This code returns all the shapes assigned to a layer even if they are in a group. Next step for me is to return the shapes on shape level, not page. I wish CreateSelection method was enough.

Yacine

I think this won't work when a child and the parent belong to the same layer.
You may consider a container (collection, array, ...) where you collect the results of the search (as object  or just their IDs) for later processing.
Yacine

wapperdude

#3
If I read your comment correctly you want All shapes in the document, that belong to a desired layer.  I can't speak for newer, Visio versions, but older ones, layers is not a searchable property.  As indicated in the referenced link, you have to recursively search all shapes, on a per page basis, check each to see if it belongs to the desired layer.  There are simple recursive code samples on the forum.

You ought to be able to adapt code povided here:  http://visguy.com/vgforum/index.php?topic=8160.0.

For newer versions of Visio, the container collection has additional membership object statements.  Not sure how amenable containers are to normal search methods.

Wapperdude
Visio 2019 Pro

metuemre

@wapperdude I actually want to return the shapes for a given layer on shape level, not page or document level.

Let's say I have two group shapes, Sheet.4 and Sheet.7 on the same page. Each group shape consist of two sub shapes, Sheet.4 has Sheet.5 and Sheet.6, Sheet.7 has Sheet.8 and Sheet.9. Only Sheet.5 and Sheet.8 are assigned to a layer named "Test". When I check Layer Properties window I see that there are 2 shapes in "Test" layer. My question is that why below code doesn't print "1" on immediate window. What is the correct use of Shape.CreateSelection method with iteration mode of visSelModeOnlySub? I also tried other iteration modes but all print "0" for sel.count.

Sub Layer_Check()
Dim testshp As Visio.Shape
Dim sel As Visio.Selection
Dim vsoLayer As Visio.Layer

Set testshp = ActivePage.Shapes("Sheet.4")
Set vsoLayer = ActivePage.Layers("Test")
Set sel = testshp.CreateSelection(visSelTypeByLayer, visSelModeOnlySub, vsoLayer)

Debug.Print sel.Count

End Sub


wapperdude

OK.  Got it.

Couple of things...
1) the createselection() expects a page object.  You cannot specify a page object.  So, what you are trying to do, won't work.
2) in your reference of 2nd reply, I can confirm that in V2007, there is a bug as reported by Nashwaan.  So, basically, the createselection() is mildly useful.  You have to recursively push thru all of the shapes on the page.  In his code, he iterates thru the selections, you could specify the desired shape and filter out all the others.

Again I don't know how this works wrt containers.

HTH
Wapperdude
Visio 2019 Pro