Irakli Darchiashvili
Irakli Darchiashvili

Reputation: 39

Visio change color of all child elements using VBA

Guys I have few elements that contain some child elements. I have to change color of some parent elements (selected by their Name) and their all sub elements (I don't know their names, nor ids, this parent elements are like black boxes) with VBA. I don't have an idea how to do this. can you help me?

Upvotes: 0

Views: 899

Answers (1)

Jon Fournier
Jon Fournier

Reputation: 4327

It's fairly easy to traverse the child shapes in a shape (especially if you're only working on one level, instead of nested children):

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

To deal with nested children, I keep a function that just recursively steps through all the children and returns a flat collection of all the child shapes. Below, with no error handling:

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

Upvotes: 1

Related Questions