Author Topic: Visio Layer Selection Problem for Shapes inside Group  (Read 181 times)

0 Members and 2 Guests are viewing this topic.

metuemre

  • Full Member
  • ***
  • Posts: 112
Visio Layer Selection Problem for Shapes inside Group
« on: December 07, 2017, 07:02:26 AM »
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?

Code: [Select]
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

  • Full Member
  • ***
  • Posts: 112
Re: Visio Layer Selection Problem for Shapes inside Group
« Reply #1 on: December 07, 2017, 08:31:37 AM »
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

  • Hero Member
  • *****
  • Posts: 2337
Re: Visio Layer Selection Problem for Shapes inside Group
« Reply #2 on: December 07, 2017, 01:43:32 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2985
  • Ideas Visio-lized into solutions
Re: Visio Layer Selection Problem for Shapes inside Group
« Reply #3 on: December 07, 2017, 05:06:45 PM »
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
« Last Edit: December 07, 2017, 05:13:59 PM by wapperdude »
Visio 2007 Std

metuemre

  • Full Member
  • ***
  • Posts: 112
Re: Visio Layer Selection Problem for Shapes inside Group
« Reply #4 on: December 08, 2017, 01:49:01 AM »
@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.

Code: [Select]
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2985
  • Ideas Visio-lized into solutions
Re: Visio Layer Selection Problem for Shapes inside Group
« Reply #5 on: December 08, 2017, 11:00:46 AM »
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 2007 Std