PowerPoint C#加载项形状分组问题

Dom*_*Dom 3 c# powerpoint add-in visual-studio-2010

我使用Visual Studio 2010开发PowerPoint 2010加载项,并且在幻灯片上对两个对象进行分组时遇到了重大问题。我正在尝试创建两个对象,将它们放在幻灯片上,并将它们全部分组在同一函数中。添加对象并将它们放置在幻灯片上不是问题。但是当涉及分组部分时...

我试过了:

PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;
string[] myRangeArray = new string[2];
myRangeArray[0] =  "nameOfShape0";
myRangeArray[1] = "nameOfShape1";
curSlide.Shapes.Range(myRangeArray).Group();
Run Code Online (Sandbox Code Playgroud)

PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;
curSlide.Shapes.Range(Array("nameOfShape0", "nameOfShape1")).Group();
Run Code Online (Sandbox Code Playgroud)

两者都惨败。我对此感到非常沮丧,并真的希望某种友善的灵魂能够为我解决。谢谢。

更新:这是我正在使用的完整代码:

PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;

PowerPoint.Shape browser = curSlide.Shapes.AddOLEObject(110, 70, 500, 400, "Shell.Explorer.2");
var slideName = "webBrowser_0";
browser.Name = slideName;

PowerPoint.Shape rectangle = curSlide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 110, 70, 500, 400);
rectangle.Name = "shape2";
string[] myRangeArray = new string[2];
myRangeArray[0] = "webBrowser_0";
myRangeArray[1] = "shape2";
curSlide.Shapes.Range(myRangeArray).Group();
Run Code Online (Sandbox Code Playgroud)

我收到的错误是“ ShapeRange对象必须包含至少两个项目”

aqu*_*nas 5

您的代码对我来说很好。尝试这个:

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 嗯...也许这是PowerPoint的一些限制。如果您运行代码并注释掉分组部分,并且您*手动*尝试将其归入powerpoint,则不允许您选择该对象。 (2认同)