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?
Asked
Active
Viewed 808 times
0
-
1Welcome to StackOverflow. Please spend some time and visit the [help center](http://stackoverflow.com/help/) to get accustomed to site. Specifically read "[How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)" and "[How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)" – Techie May 24 '16 at 12:16
1 Answers
1
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

Jon Fournier
- 4,299
- 3
- 33
- 43