Getting the count of list members of a container shape

Started by Visisthebest, August 21, 2023, 08:43:10 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

For the shapesheet there is the handy:

LISTMEMBERCOUNT()

https://learn.microsoft.com/en-us/office/client-developer/visio/listmembercount-function

To get the number on shapes that are in the container's list. For VBA I cannot find a function that does the same unfortunately, am I overlooking a VBA object method or property that provides the count?
Visio 2021 Professional

wapperdude

#1
I can think of 2 ways, but have not tried either. 
1) use cells.result method to get shapesheet value.  To universally use this with code, use the code to...
      a) create a User Section entry to hold shapesheet formula
      b) read the value with cells.result
      c) delete entry if desired

2) try this more direct approach: https://learn.microsoft.com/en-us/office/vba/api/visio.containerproperties.getmembershapes

A list is a specialized container.  ...in theory.
Visio 2019 Pro

wapperdude

#2
Oooops.  Correction to method 2 above.  See https://learn.microsoft.com/en-us/office/vba/api/visio.containerproperties.getlistmembers

However, neither of these produce a member count directly.
My bad.
Visio 2019 Pro

Visisthebest

Thank you Wapperdude yes I use GetMemberShapes frequently, but I am just surprised there is no count property anywhere I can check.

Your first solution is a clever workaround thank you!

(using GetMemberShapes across the interop in an add-in is really wasteful to get the array to get the count, so your first solution is better I think)
Visio 2021 Professional

wapperdude

#4
Yep.  No counting.

But you could do it, basically in 3 steps....not counting Dim declarations:

        Dim vsoContainerShape As Visio.Shape

        Set vsoContainerShape = ActivePage.Shapes.ItemFromID(15)    'get list container
        arr = vsoContainerShape.ContainerProperties.GetListMembers    'define member array
        Debug.Print UBound(arr) + 1                         'This is the count; add 1 because 1st object is 0

Visio 2019 Pro

Visisthebest

Or put the LISTMEMBERCOUNT shapesheet function in a user cell then read that cell with code. I think that code is faster.

https://learn.microsoft.com/en-us/office/client-developer/visio/listmembercount-function

Visio 2021 Professional

Yacine

The are indeed rare cases where writing a temporary formula in a shapesheet helps, but in your case you should be better off with the functions GetMemberShapes or GetListMembers.
Yacine