none
Getting "System.UnauthorizedAccessException HResult=0x80070005" when I try to insert second group shape on slide [c#] RRS feed

  • Question

  • Hello, 

    I have add-in where one button makes following steps:

    - insert 2 shapes

    - group them as one group set.

    So, when I push the button for the 1st time - everything is OK. When I push it for the 2nd time I have an error 

    System.UnauthorizedAccessException
      HResult=0x80070005
      Message=Grouping disabled for selected shapes
      Source=FirstPPTAddIn
      StackTrace:
       at Microsoft.Office.Interop.PowerPoint.ShapeRange.Group()
       at FirstPPTAddIn.MyRibbon.OnShapesButton(IRibbonControl control) in C:[...]\Visual Studio 2017\Projects\FirstPPTAddIn\FirstPPTAddIn\MyRibbon.cs:line [# line]

    At this time there are the 1st group shape and 2 separate shapes whithout grouping.

    I tried to add two additional lines for selection those shapes, but had the same error. 

    My goal is to be able to insert unlimited amount of group shapes on the slide.

    Can you explain why this error appears and how I can fix it? Thanks!

    My code is

    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
                PowerPoint.Slide curSlide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
    [...]
    
    PowerPoint.Shapes shapes = curSlide.Shapes;
                List<string> list = new List<string>();
                list.Add("shape1");
                list.Add("shape2");
                shape1.Select(Office.MsoTriState.msoTrue);
                shape2.Select(Office.MsoTriState.msoFalse);
                PowerPoint.Shape groupShape = curSlide.Shapes.Range(list.ToArray()).Group(); //Here the Error appears

    • Edited by Lena Erik Sunday, October 21, 2018 3:31 PM
    Sunday, October 21, 2018 3:30 PM

Answers

  • Hi Lina, 

    You're right, this code is OK for insert and copy one group set. My goal is insert the one group, then insert the second and so on by clicking the button. Maybe I was not so clear in my original post. Sorry.

    Anyway, thanks for suggestions, Lina!

    So, 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++;
    }
    • Marked as answer by Lena Erik Tuesday, October 23, 2018 7:17 PM
    Tuesday, October 23, 2018 7:16 PM

All replies

  • Hi Lena,

    Please refer to the code as below:

    private void ThisAddIn_Startup(object sender, System.EventArgs e) {
        this.Application.PresentationNewSlide += Application_PresentationNewSlide;
    }
    
    void Application_PresentationNewSlide(PowerPoint.Slide Sld) {
        PowerPoint.Shape textBox = Sld.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 0, 0, 500, 50);
        textBox.Name = "shape1";
        textBox.TextFrame.TextRange.InsertAfter("This text was added by using code.");
    
        textBox = Sld.Shapes.AddTextbox(Office.MsoTextOrientation.msoTextOrientationHorizontal, 0, 100, 500, 50);
        textBox.TextFrame.TextRange.InsertAfter("This text was also added by using code.");
        textBox.Name = "shape2";
    
        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();
    }

    For more information, please review the following link:

    powerpoint c# add-in shape grouping issue

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, October 22, 2018 5:20 AM
  • Hi Lina,

    Thank you for response. 

    I used your code as base for mine and I had the same issues as well in the last line of code:(

    Monday, October 22, 2018 8:55 AM
  • Hi Lena,

    Where is your final code different from the following code?

    According to my tests, The following code is works fine:

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.PresentationNewSlide += Application_PresentationNewSlide;
            }
            void Application_PresentationNewSlide(PowerPoint.Slide Sld)
            {
                PowerPoint.Shape textBox = Sld.Shapes.AddShape(Office.MsoAutoShapeType.msoShape10pointStar,100,100,100,100);
                textBox.Name = "shape1";
                textBox.TextFrame.TextRange.InsertAfter("This text was added by using code.");
    
                textBox = Sld.Shapes.AddShape(Office.MsoAutoShapeType.msoShape10pointStar, 100, 100, 100, 100);
                textBox.TextFrame.TextRange.InsertAfter("This text was also added by using code.");
                textBox.Name = "shape2";
    
                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();
            }

    The screenshot is the result of the work:

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 23, 2018 5:47 AM
  • Hi Lina, 

    You're right, this code is OK for insert and copy one group set. My goal is insert the one group, then insert the second and so on by clicking the button. Maybe I was not so clear in my original post. Sorry.

    Anyway, thanks for suggestions, Lina!

    So, 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++;
    }
    • Marked as answer by Lena Erik Tuesday, October 23, 2018 7:17 PM
    Tuesday, October 23, 2018 7:16 PM