none
How to group a number of shapes on another PowerPoint.Slide? RRS feed

  • Question

  • I want to group a number of shapes in another slide. The task seems very simple. However, the problem is the shapes needed to be grouped is not on the current slide.

    For example,

    The current slide is slide number 2. My shapes needed to be grouped are on the slide number 10. When a shape on the slide number 10 get selected, it always causes error. It seems that, we cannot change the slide selection.

    My code is follow:

    PowerPoint.Slides allSlides = ppApp.ActivePresentation.Slides;

    for (int i = 1; i <= allSlides.Count; i++)

    {

    PowerPoint.Slide slide = allSlides[i];

    if (slide.SlideId == 10)

    {

    //- Select the slide first

    slide.Select();

    //- Select (assuming) shape1 and shape3

    slide.Shapes[1].Select(); //- Error: The shape1 cannot be selected????

    slide.Shapes[3].Select(Microsoft.Office.Core.MsoTriState.msoFalse);

         PowerPoint.ShapeRange shapeRange = ppApp.ActiveWindow.Selection.ShapeRange;

                         shapeRange.Group();

    }

    }


    • Edited by chipbk11 Wednesday, December 3, 2014 4:27 PM
    Wednesday, December 3, 2014 9:45 AM

Answers

  • The following idea, I borrow from Andrei , who is working at Add-In-Express

     int slideIndex = 10;

     //- go to the slide you want

     PowerPoint.Slide slide = ppApp.ActivePresentation.Slides[slideIndex];

     PowerPoint.Shapes shapes = slide.Shapes;

     //- assume that we have 2 shapes on slide 10 with names are Name1, Name2

     List<string> names = new List<string>();

     names.Add("Name1");

     names.Add("Name2");

     //- Get the shape range from the name list.

     PowerPoint.ShapeRange shapeRange = shapes.ShapeRange(names.ToArray()); //- or you can use Array("Name1", "Name2"). However, using a list is more flexible.

     PowerPoint.Shape groupShape = shapeRange.Group();

     //- Release Com Objects, when we get done

     Marshal.ReleaseComObject(slide); slide = null;

     Marshal.ReleaseComObject(allShapes); allShapes = null;

     Marshal.ReleaseComObject(shapeRange); shapeRange = null;

     Marshal.ReleaseComObject(groupShape); groupShape = null;

    A ShapeRange can be achieved from the array of shape index. However, given a specific shape, we don't know its index. The following function seems to be awkward to achieve the shape index:

     

     //- return the shape index. Return -1 in the case of unknown 

     private static int GetShapeIndex(PowerPoint.Shape shape)

     {

      object parent = null;

            try { parent = shape.Parent; } catch {parent = null; }

            if (parent == null) return -1;

            if ( !(parent is PowerPoint.Slide) ) return -1;

            int shapeIndex = -1;

            PowerPoint.Slide slide = (parent as PowerPoint.Slide);

            PowerPoint.Shapes shapes = slide.Shapes;

            for (int i = 1; i <= shapes.Count; i++)

            {

                   PowerPoint.Shape aShape = shapes[i];

                   if (aShape.Id == shape.Id) { shapeIndex = i; break; }

                   Marshal.ReleaseComObject(aShape); aShape = null;               

            }

            Marshal.ReleaseComObject(shapes); shapes = null;

            Marshal.ReleaseComObject(slide); slide = null;

     }


    Thursday, December 4, 2014 9:33 AM

All replies

  • Try to call the Select method of the Slide class before selecting shapes.
    Wednesday, December 3, 2014 4:24 PM
  • Thanks. I did call slide.Select() in my above piece of codes. But it doés not work.

    Anyway, I did solve my problem with another approach. Thanks. Could you please close this topic?



    • Edited by chipbk11 Wednesday, December 3, 2014 4:35 PM
    Wednesday, December 3, 2014 4:26 PM
  • Could you please share the solution for other forum readers?
    Wednesday, December 3, 2014 5:09 PM
  • The following idea, I borrow from Andrei , who is working at Add-In-Express

     int slideIndex = 10;

     //- go to the slide you want

     PowerPoint.Slide slide = ppApp.ActivePresentation.Slides[slideIndex];

     PowerPoint.Shapes shapes = slide.Shapes;

     //- assume that we have 2 shapes on slide 10 with names are Name1, Name2

     List<string> names = new List<string>();

     names.Add("Name1");

     names.Add("Name2");

     //- Get the shape range from the name list.

     PowerPoint.ShapeRange shapeRange = shapes.ShapeRange(names.ToArray()); //- or you can use Array("Name1", "Name2"). However, using a list is more flexible.

     PowerPoint.Shape groupShape = shapeRange.Group();

     //- Release Com Objects, when we get done

     Marshal.ReleaseComObject(slide); slide = null;

     Marshal.ReleaseComObject(allShapes); allShapes = null;

     Marshal.ReleaseComObject(shapeRange); shapeRange = null;

     Marshal.ReleaseComObject(groupShape); groupShape = null;

    A ShapeRange can be achieved from the array of shape index. However, given a specific shape, we don't know its index. The following function seems to be awkward to achieve the shape index:

     

     //- return the shape index. Return -1 in the case of unknown 

     private static int GetShapeIndex(PowerPoint.Shape shape)

     {

      object parent = null;

            try { parent = shape.Parent; } catch {parent = null; }

            if (parent == null) return -1;

            if ( !(parent is PowerPoint.Slide) ) return -1;

            int shapeIndex = -1;

            PowerPoint.Slide slide = (parent as PowerPoint.Slide);

            PowerPoint.Shapes shapes = slide.Shapes;

            for (int i = 1; i <= shapes.Count; i++)

            {

                   PowerPoint.Shape aShape = shapes[i];

                   if (aShape.Id == shape.Id) { shapeIndex = i; break; }

                   Marshal.ReleaseComObject(aShape); aShape = null;               

            }

            Marshal.ReleaseComObject(shapes); shapes = null;

            Marshal.ReleaseComObject(slide); slide = null;

     }


    Thursday, December 4, 2014 9:33 AM