Visio Guy

Visio Discussions => Programming & Code => Topic started by: wapperdude on January 25, 2019, 03:59:44 AM

Title: Containers: Finding Them, Finding their members
Post by: wapperdude on January 25, 2019, 03:59:44 AM
Now that I'm a bona fide, thoroughly modern Visio user, I decided to explore containers.  Yeah, old stuff now as they came on the scene in 2010.  But, what I noticed is that the useful code examples on the web are, like, not there.  There's technical descriptions, but, no examples to go with them.  So, here's some really basic, fundamental, working code. 

And why is it that Lists, i.e., specialized containers, are not provided on the GUI?  Well, too specialized.  They are sort of hidden, provided as shapes on various stencils.  Well, actually, they're well hidden.  But, you can customize a shape and turn it into a Container or a List or a Callout.  See 2nd link below.

Also, here are some links:
https://www.youtube.com/watch?v=UNhxdSRGAmc (https://www.youtube.com/watch?v=UNhxdSRGAmc)
https://blogs.msdn.microsoft.com/visio/2010/01/12/custom-containers-lists-and-callouts-in-visio-2010/ (https://blogs.msdn.microsoft.com/visio/2010/01/12/custom-containers-lists-and-callouts-in-visio-2010/)
https://support.office.com/en-us/article/Clarify-the-structure-of-diagrams-by-using-containers-and-lists-5A3F0608-A691-4EEF-8538-D3DC1141AE85 (https://support.office.com/en-us/article/Clarify-the-structure-of-diagrams-by-using-containers-and-lists-5A3F0608-A691-4EEF-8538-D3DC1141AE85)
https://code.msdn.microsoft.com/windowsdesktop/Visio-2010-Add-Containers-e8f8daf2 (https://code.msdn.microsoft.com/windowsdesktop/Visio-2010-Add-Containers-e8f8daf2)
https://social.technet.microsoft.com/Forums/en-US/4b1eaf5a-11e9-44af-b0e8-28367a51491f/sample-code-containerpropertiesgetlistmembers (https://social.technet.microsoft.com/Forums/en-US/4b1eaf5a-11e9-44af-b0e8-28367a51491f/sample-code-containerpropertiesgetlistmembers)
http://www.visguy.com/2010/06/05/using-visio-2010-containers-to-create-server-drilldowns-with-excel-data/ (http://www.visguy.com/2010/06/05/using-visio-2010-containers-to-create-server-drilldowns-with-excel-data/)
http://visguy.com/vgforum/index.php?topic=6217.0 (http://visguy.com/vgforum/index.php?topic=6217.0)

Well, I hope this helps make your container adventure a little easier and more productive.

Wapperdude

Title: Re: Containers: Finding Them, Finding their members
Post by: rezingg on February 04, 2022, 01:22:56 AM
Is there a simple way to find if a given shape is part of a container, and if it is, then get the container?
I see examples starting at the page and drilling down from there, but how about starting at a shape.
The only thing I found is parsing the "Relationships" cell in the ShapeSheet of the shape for "DEPENDSON(4,..". But MS documentation says that entry isn't really what drives the relationship between two shapes, but is more of a note...

Any pointers are greatly appreciated. Thanks!
Title: Re: Containers: Finding Them, Finding their members
Post by: Yacine on February 04, 2022, 08:57:42 AM
https://docs.microsoft.com/en-us/office/vba/api/Visio.Shape.ContainingShape
Title: Re: Containers: Finding Them, Finding their members
Post by: wapperdude on February 04, 2022, 03:13:57 PM
Adding to Yacine's reply...there are a few additional properties, such as this, re ContainerID:  https://docs.microsoft.com/en-us/office/vba/api/visio.shape.containingmasterid (https://docs.microsoft.com/en-us/office/vba/api/visio.shape.containingmasterid)
Title: Re: Containers: Finding Them, Finding their members
Post by: Visisthebest on February 04, 2022, 05:36:02 PM
Very useful thank you for the overview Wapperdude! (I use containers regularly)
Title: Re: Containers: Finding Them, Finding their members
Post by: wapperdude on February 04, 2022, 06:09:13 PM
LOL.   In fact, I rarely use them.
Title: Re: Containers: Finding Them, Finding their members
Post by: rezingg on February 04, 2022, 06:16:21 PM
I did not give this a try yet, but I think what has been referenced here is ''ownership" of a shape (i.e. has-a relationship). But I am asking about containers in Visio (hence I posted in this thread). A container does not own the shapes, they are owned by the page or a group. I believe above mentioned calls would return the owner, i.e. the page or the group, but not the container. Shapes always have an owner, but being member of a container is optional. And a shape can be member of multiple containers. A shape that is member of a container does not show up as a child in the container shape (and the container is not the parent of the shape).
Or maybe I'm completely wrong and I should first give this a try...
Title: Re: Containers: Finding Them, Finding their members
Post by: wapperdude on February 04, 2022, 09:16:22 PM
I think you should give it a try.
Title: Re: Containers: Finding Them, Finding their members
Post by: Visisthebest on February 04, 2022, 09:39:46 PM
They are great for 'visually grouping' shapes in a large diagram, and it is great the user can easily drag shapes in and out of them (and drag a visual group of shapes by dragging the container). I would really miss them if they weren't there.
Title: Re: Containers: Finding Them, Finding their members
Post by: rezingg on February 04, 2022, 09:57:09 PM
So far ContainingShape() seems to return the parent, and not the container. Here is what I tried:
1)  I defined the following sub:
Sub ShowContainingShape()
    Debug.Print "---"
    Debug.Print Application.ActiveWindow.Selection.PrimaryItem.NameU
    Debug.Print Application.ActiveWindow.Selection.PrimaryItem.ContainingShape().NameU
End Sub

2) I created a container on my page, and a rectangle. The container I named myContainer, and the rectangle myRectangle.
3) For myRectangle I set above Sub to execute when it is double-clicked (Developer > Behavior > Double-Click > Run Macro)

Now, when I double-click myRectangle I see below message in the "Immediate" window of the Visual Basic Editor

---
myRectangle
ThePage

And I see the same message independent of myRectangle being inside the container, or outside of it. It always reports the owner, the parent, and not any container it is associated with. Otherwise it should report myContainer instead of ThePage.

What am I missing?
Title: Re: Containers: Finding Them, Finding their members
Post by: wapperdude on February 06, 2022, 12:04:14 AM
Some of your code lines don't have proper syntax, but, with corrected syntax, results were the same.  I'd say there appears to be a bug in the containing shape method.

Since I rarely use containers, actually, I dislike them, I can merely suggest following method...
   1) select your shape of interest
   2) loop thru all containers on a page
   3) loop thru members of each container and compare with selected shape
   4) identical compare means current container is the desired one.

Not elegant, but ought to work.  Seems like there ought to be an easier, more efficient method.
Title: Re: Containers: Finding Them, Finding their members
Post by: Croc on February 06, 2022, 07:38:55 AM
ContainingShape works with groups, not containers. Just not very correct name :)
To work with containers, you need use MemberOfContainers. Like that
Sub ShowContainingShape()
    Debug.Print "---"
    Debug.Print Application.ActiveWindow.Selection.PrimaryItem.NameU
    a = Application.ActiveWindow.Selection.PrimaryItem.MemberOfContainers()
    For i = LBound(a) To UBound(a)
        Debug.Print ActivePage.Shapes.ItemFromID(a(i)).NameU
    Next
End Sub
Title: Re: Containers: Finding Them, Finding their members
Post by: rezingg on February 08, 2022, 05:36:56 PM
Quote from: Croc on February 06, 2022, 07:38:55 AM
ContainingShape works with groups, not containers. Just not very correct name :)
To work with containers, you need use MemberOfContainers. Like that
Sub ShowContainingShape()
    Debug.Print "---"
    Debug.Print Application.ActiveWindow.Selection.PrimaryItem.NameU
    a = Application.ActiveWindow.Selection.PrimaryItem.MemberOfContainers()
    For i = LBound(a) To UBound(a)
        Debug.Print ActivePage.Shapes.ItemFromID(a(i)).NameU
    Next
End Sub

Thank you @Croc, this is exactly what I was looking for!
Title: Re: Containers: Finding Them, Finding their members
Post by: rezingg on March 11, 2022, 12:18:14 AM
I'm having a hard time with a=shape.MemberOfContainers(), when the shape is part of a group. The returned value "a" does not seem to be a valid array, so operation on it causes an error...
Any help would be greatly appreciated.