Find all shapes based on given master

Started by rezingg, December 09, 2021, 02:24:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rezingg

Is there a simple way to find all shapes on a page that are based on a given master from the document stencil?
With 'simple' I mean besides iterating through all shapes on a page and recursively descending down into groups.

Thanks!

JohnGoldsmith

Hi,


You can use Page.CreateSelection where you pass in an instance of your target master as the 'Data' argument.


See this link for details:


https://docs.microsoft.com/en-us/office/vba/api/visio.page.createselection


Best regards


John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Visisthebest

Visio 2021 Professional

rezingg

Excellent! I'll give this a try. Thanks John!

rezingg

Looks like with Page.CreateSelection I still need to descend down into groups to find all masters. Out of the box the method won't select shapes that are contained in a group.
Also, do you have any insight as to what the second parameter does? The short description given in Microsoft Help isn't very explanatory...e.g. "Selection reports only subselected shapes."... what are subselected shapes?

Surrogate

Quote from: rezingg on December 10, 2021, 10:05:07 PM
Out of the box the method won't select shapes that are contained in a group.
You need use recursion!

Quote from: rezingg on December 10, 2021, 10:05:07 PMAlso, do you have any insight as to what the second parameter does? The short description given in Microsoft Help isn't very explanatory...e.g. "Selection reports only subselected shapes."... what are subselected shapes?
Hope this article can helps: Detect Sub-selected Shapes Programmatically

rezingg

Thanks surrogate. That article helped a lot. Surprised Google didn't find it.
Though I still don't understand why I can pass the iteration mode to CreateSelection method, but it will never select shapes in a group. What is the point to pass the iteration mode?

JohnGoldsmith

Hi,
I think it depends on the scope of the parent collection.  For example Page.Shapes doesn't return a collection of all shapes in the page, only the top level shapes.  As Surrogate suggests you would need to recurse throw each top level shape to retrieve each descendent. 

The CreateSelection method also exists at Shape level, however, slightly unintuitively, it reports only siblings (unlike Page.CreateSelection that reports child shapes) and so you need to dip into that sub-shape collection to use it. 

For example, here's a C# example (same thing in VBA):

Code (c#) Select
//Get hold of the first sub-shape of Sheet.6 (a group shape)
var shp = vPag.Shapes.ItemFromID[6].Shapes[1];

//Get sub-shape selection
var subSel = shp.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll);


If you have master instances inside other master instances then I think you'll need to break up your selection process by first of all getting your parents selection and then iterating through each of those to get the child ones.
Best regards
John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/