title: VISIO VBA Get a collection of all Shapes in a group
body: <p>I want to get a collection of all shapes that are in a certain group. Shapes in the group may be a group themselves so I (think I) need a recursive function to get every shape.</p>
  
  <p>This is my code:</p>
  
  <pre><code>Function getAllShapes(shp As Shape) As Collection
      Dim shapes As Collection
      Dim subshp As Shape
      Dim colShp As Shape
      Dim col As Collection
  
      If shp.Type = 2 Then 'Check if shp is a group
          For Each subshp In shp.shapes
              shapes.Add (subshp) 'Add the subshp to the shape collection
              If subshp.Type = 2 Then 'Check if subshp is group
                  Set col = getAllShapes (subshp) 'Get all the shapes from the group
                  For Each colShp In col 'Error here
                      shapes.Add (colShp) 'Add all the shapes to the collection
                  Next colShp
              End If
          Next subshp
      Else: shapes.Add (shp)
      End If
  
      Set getAllShapes = shapes
      Set shapes = Nothing
  
  End Function
  </code></pre>
  
  <p>However, I always get a runtime error: Object Required whenever I try to access the collection from the function (for example in the function itself)</p>