c# 在幻灯片上插入多个形状组时出错

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

UPD:也许有帮助。这是我的错误的详细信息:

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

我将我的加载项添加到 Exeption Settings,当我为第二组设置 运行 代码时,我在幻灯片上得到了两个额外的形状,但没有分组。我不明白为什么最后一行代码不起作用。 我可以"copy-past"第一组设置很多次并对其进行更改,但我需要通过按钮添加它们。


我用了this code for grouping shapes。但它只允许放置一个形状组。我需要更改什么代码才能在一张幻灯片上插入无限形状组?

部分代码

    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();

当我尝试插入第二个形状组时,最后一行出现错误 System.UnauthorizedAccessException: "grouping is disabled for selected shapes"

谢谢!

这可能是由于添加形状 1

后文件已经在 use.Try 中手动转储 powerpoint.exe

UnauthorizedAccessException 可能是以下原因之一:

  • 来电者没有所需的权限。
  • 该文件是正在使用的可执行文件。
  • Path 是一个目录。
  • 路径指定了一个只读文件。

我的一个朋友今天解决了这个问题。他为数组中的形状添加了计数器。以下部分代码

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++;
}