VBA - select all shapes in container

Started by sayre, February 14, 2016, 10:48:27 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sayre

Hello I posted a question in network diagram section just now, but it maybe should be here instead.

http://visguy.com/vgforum/index.php?topic=7045.0

Can anyone assist?

I'm working on a network diagram project and having trouble figuring out how to select all current members of a visio container with VBA.

I have a macro in excel that reads large lists of network connections and draws visio diagrams from it. This seems simple enough but I can't seem to nail it.

First I tried recording myself right-clocking on the container and choosing >>container >>"select contents" but the resulting code simply selects each shape by ID one at a time (presumably because this command already knows how many shapes there are in conatiner and their IDs).

I need code that works without me knowing this, hope that makes sense. I did find a function that iterates through all members of a container and makes a collection of each ID, but I can't make the next leap to use this to select all of these shapes. I'm probably missing something easy, any help much appreciated. Using Visio 2010 and Excel 2010. Here's the appropriate portion of the code, and the spot where I think I need help:




         ' get an enumerable list of shape ids that are already in the container
        Dim colMembers As Collection
        Set colMembers = getMembersOfContainer(vsoContainerShape)
       
        Dim shapeSelection As Selection
       
        If 0 < colMembers.Count Then
            Dim intX As Integer
            For intX = 1 To colMembers.Count
                If colMembers.Item(intX) = visShape.ID Then
               
               
                "some code that adds each vis.Shape to the current selection"

               
                    Exit For
                End If
            Next intX
        End If


above code uses this function:

    '  pass in a container and return a collection of shapeIds
    '
    Public Function getMembersOfContainer _
                (ByRef vsoContainerShape As Visio.Shape) _
                As Collection

        On Error GoTo ErrHandler

        Dim colReturn As Collection
        Set colReturn = New Collection
       
        Dim arrMember() As Long
        arrMember = vsoContainerShape.ContainerProperties.GetMemberShapes(VisContainerFlags.visContainerFlagsDefault)
       
        Dim memberId As Long
        Dim intI As Integer

        For intI = 0 To UBound(arrMember)
            colReturn.Add (arrMember(intI))
        Next
       
        Set getMembersOfContainer = colReturn
        Exit Function
ErrHandler:
        Debug.Print "getMembersOfContainer " & Err.Description
        Set getMembersOfContainer = colReturn

    End Function
[\code]



JuneTheSecond

#1
I am new to Container,
So I searched a hint in the web, and found ContainerProperties.GetMemberShapes Method at
https://msdn.microsoft.com/ja-jp/library/office/ff766306.aspx .
There is an example.
This example is not almost ready to run.
Then I made an macro from this example.
And I found ContainerProperties.GetMemberShapes Method really returns the contained shapes IDs.
I changed the name of container into Frame1, but you don't need it.


Option Explicit

Sub test()
    Dim memberID As Variant
    Dim vsoContainerShape As Visio.Shape
    Dim vsoShape As Visio.Shape
   
    Set vsoContainerShape = ActivePage.Shapes("Frame1")
    For Each memberID In vsoContainerShape.ContainerProperties.GetMemberShapes(visContainerFlagsDefault)
        Set vsoShape = ActivePage.Shapes.ItemFromID(memberID)
        Debug.Print vsoShape.ID
    Next
End Sub

Best Regards,

Junichi Yoda
http://june.minibird.jp/