Search code examples
c#powerpointadd-in

c# Error with inserting more than one shape group on the slide


UPD: Maybe it helps. This is details of my Error:

System.UnauthorizedAccessException HResult=0x80070005
Message=Grouping disabled for selected shapes (Для выделенных фигур группирование отключено). Source=FirstPPTAddIn StackTrace: at Microsoft.Office.Interop.PowerPoint.ShapeRange.Group() at FirstPPTAddIn.MyRibbon.OnShapeButton(IRibbonControl control) in D:\Documents\Visual Studio 2017\Projects\FirstPPTAddIn\FirstPPTAddIn\MyRibbon.cs:line 84

I added my add-in to Exeption Settings and when I've run code for the second group set I got two additional shapes on the slide without grouping. I don't understand why the last line of code doesn't work. I can just "copy-past" the first group set many times and make changes with them, but I need add them by button.


I used this code for grouping shapes. But it allows to put just one shape groups only. What I need to change in code for inserting unlimited shape groups on the one slide?

Part of code

    PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
    PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;
    string[] myRangeArray = new string[2];
    myRangeArray[0] = "shape1";
    myRangeArray[1] = "shape2";
    curSlide.Shapes.Range(myRangeArray).Group();

When I try to insert second shape group I have an error in the last line says System.UnauthorizedAccessException: "grouping is disabled for selected shapes".

Thanks!


Solution

  • A friend of mine solved this problem today. He add counter for the shapes in Array. The part of code below

    private int count = 0;
    public void OnButton(Office.IRibbonControl control)
    {
         var shape1Name = "shape1" + count;
         var shape2Name = "shape2" + count;
    ...
         shape1.Name = shape1Name;
         shape2.Name = shape2Name;
    ...
         string[] myRangeArray = new string[2];
         myRangeArray[0] = (shape1Name);
         myRangeArray[1] = (shape1Name); 
         curSlide.Shapes.Range(myRangeArray).Group(); 
         count++;
    }