Visio 使用 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

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

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

最新更新