0

I need to get all the shapes in active presentation from all slides, including the shapes in grouped items in C#.

I need all shapes returned in List or Array(Shape).

enter image description here

Chandraprakash
  • 773
  • 1
  • 10
  • 18

1 Answers1

1

You can enumerate shapes of a slide via Shapes property. Similarly you can enumerate child shapes via GroupItems property (only for msoGroup shape type). To put that together:

public static IEnumerable<Shape> EnumerateShapes(Presentation presentation)
{
    return presentation.Slides.Cast<Slide>().SelectMany(slide =>
        EnumerateShapes(slide.Shapes.Cast<Shape>()));
}

public static IEnumerable<Shape> EnumerateShapes(IEnumerable<Shape> shapes)
{
    foreach (Shape shape in shapes)
    {
        yield return shape;
        if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup)
        {
            foreach (var subShape in EnumerateShapes(shape.GroupItems.Cast<Shape>()))
                yield return subShape;
        }
    }
}

Note that this kind of recursion comes at its cost and maybe it would be wise to convert the above method to non-recursive one.

Peter Wolf
  • 3,700
  • 1
  • 15
  • 30