Visio VBA 尝试获取容器和成员形状的列表

Visio VBA trying to get a list of containers and member shapes

背景: 我有一些代码贯穿 Visio 页面和 returns 所有形状。其中很多形状都在容器中,所以我想知道一个形状属于哪个容器。

原始方法:我希望检索每个形状的"parent"容器(我只需要一层容器,容器内没有容器)使用Shape.ContainingShape 属性 但是每个形状只返回“0”。

如果有人对我最初尝试获取容器的方式有解决方案,那将是最优雅的。但由于我无法让它工作,我正在尝试以下替代方案,这也是一个障碍。

当前方法: 我能够获得页面上所有容器的列表,现在我想拉出每个容器的成员形状。它不是那么干净,但它可以让我交叉引用形状并获取它们所属的容器。

问题: 当我试图创建一个数组时,我得到 "Error 13 Type Mismatch",其中第 0 列是容器名称,第 1 列是成员形状。

' Create array of containers and member shapes
Dim arr() As Long
Dim vsoMemberShape As Shape
Dim vsoContainerShape As Shape
Dim containerArr() As Long
Dim rows As Integer
Dim i As Integer

For Each ContainerID In vsoPage.GetContainers(visContainerIncludeNested)

    Set vsoContainerShape = vsoPage.Shapes.ItemFromID(ContainerID)
    arr = vsoContainerShape.ContainerProperties.GetMemberShapes(1)
    rows = UBound(arr)
    ReDim containerArr(0 To rows, 0 To 1)
    For i = 0 To UBound(arr)

        Set memberShape = vsoPage.Shapes.ItemFromID(arr(i))
        containerArr(i, 0) = vsoContainerShape.NameU
        containerArr(i, 1) = vsoMemberShape.NameU

    Next

Next

' The following code is in a For loop, not shown
' shapeToName is what I want to compare to the member shapes in the container 
' array defined above, and then retrieve the corresponding container
' This is where the error is popping up
shapeToName = CStr(vsoShapeTo.Name)

Dim x As Integer
x = Application.Match(shapeToName, Application.Index(containerArr, 0, 1), 0)
shapeContainer = containerArr(x, 1)

我认为您要查找的是 Shape.MemberOfContainers,其中 returns 形状所属的容器数组。

你也可以看看这个post,它涵盖了同样的问题:

我还将在 David Parker 的 link 到 post 中加入跨职能流程图上下文中的容器,它充分利用了容器和列表:

https://bvisual.net/2009/09/07/visio-2010-containment-and-cross-functional-flowcharts/