Visio 使用 VBA 更改所有子元素的颜色

Visio change color of all child elements using VBA

伙计们,我有几个包含子元素的元素。我必须使用 VBA 更改某些父元素(由它们的名称选择)及其所有子元素(我不知道它们的名称,也不知道 ID,这个父元素就像黑框)的颜色。我不知道该怎么做。你能帮帮我吗?

遍历形状中的子形状相当容易(特别是如果您只在一个级别上工作,而不是嵌套的子形状):

Dim ParShp as Visio.Shape
Set ParShp = ActivePage.Shapes("ShapeName")
Dim ShpObj as Visio.Shape
For Each ShpObj in ParShp.Shapes
    ShpObj.CellsU("FillForegnd").FormulaU = "RGB(0,0,0)"
Next ShpObj

为了处理嵌套的子项,我保留了一个函数,它只是递归地遍历所有子项,returns 是所有子项形状的平面集合。下面,没有错误处理:

Public Function GetAllSubShapes(ShpObj As Visio.Shape, SubShapes As Collection, Optional AddFirstShp As Boolean = False)
    If AddFirstShp Then SubShapes.Add ShpObj
    Dim CheckShp As Visio.Shape
    For Each CheckShp In ShpObj.Shapes
        SubShapes.Add CheckShp
        Call GetAllSubShapes(CheckShp, SubShapes, False)
    Next CheckShp
End Function