Finding a the container which a shape belongs to.

Started by bwharrington, September 06, 2019, 05:05:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bwharrington

Without iterating over all the shapes on a page and then the member shapes of a particular shape, is there better way to know if a shape is within a container?

I though there was a property or method on the shape but I dont see one. Its possible I mistook this for something else.

Thanks,

Paul Herber

The shape refers to the container it is in with the cell:
Relationships in the Shape Layout section of the shapesheet.
It'll be a formula something like:
=SUM(DEPENDSON(4,Plain!SheetRef()))

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

wapperdude

For vba solution, there's this:  https://docs.microsoft.com/en-us/office/vba/api/Visio.ContainerProperties.GetMemberShapes.  It works based upon a container and gives its members.

Find more links here:  http://visguy.com/vgforum/index.php?topic=8697.0

So, if you have specific shape in question, use Paul's method.
If you have specific container, use above method. 

If its a general inquiry, you need to iterate.
Visio 2019 Pro

Visisthebest

Paul thank you, this is much faster for finding out a shape's container membership.

I see the first cell reference in DEPENDSON is always 4, the second is logically pointing to the container the shape is a member of.

What is the 4 referring to?
Visio 2021 Professional

Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

Thank you Paul that is a very informative page didn't know it existed!
Visio 2021 Professional

VisExplorer22

Hey Paul,

Thanks for sharing all the details here but I am not able to use the below formula in VBA code.

=SUM(DEPENDSON(4,Plain!SheetRef()))

Is below the right method to do it? I am at a very beginner level on Visio VBA so excuse my naivety here :)

Dim szPreShpDesc As Visio.Shape
                   
szPreShpDesc.Cells.Formula = SUM(DEPENDSON(4, Plain!SheetRef()))

Any help here will be greatly appreciated.

Paul Herber

You have to replace the 'Plain' in the formula with the shape name you are using.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

JohnGoldsmith

Hi,
Also, Shape.MemberOfContainers will return an array of shape IDs corresponding to the Container shapes this shape is a member of.  You could then try and parse the Relationships cell, as per Paul's point, if you wanted to inspect the type of relationship.
You could first get hold of the (container) shape IDs, and from there get the local name that's used in the formula.
ie Page.Shapes.ItemFromID(containerId).Name

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

wapperdude

@VisExplorer22:  The formula that Paul presents is a shapesheet formula.  It is not VBA compatible.  For VBA, see my 1st link in reply #2.
Visio 2019 Pro