I developing a PowerPoint 2010 add-in using Visual Studio 2010 and am having significant issues with grouping two objects on a slide. I am trying to create the two objects, place them on the slide and group them all in the same function. Adding the objects and placing them on the slide is not an issue. When it comes to the grouping part though....
I have tried:
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();
and
PowerPoint._Application myPPT = Globals.ThisAddIn.Application;
PowerPoint.Slide curSlide = myPPT.ActiveWindow.View.Slide;
curSlide.Shapes.Range(Array("nameOfShape0", "nameOfShape1")).Group();
Both of which fail miserably. I am getting quite frustrated with this and am really hoping some kind soul has a solution for me. Thanks.
Update: Here is the full code I am using:
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();
The Error I am receiving is "The ShapeRange object must contain at least two items"
Your code worked fine for me. Try this:
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();
}
If the layout of the slide you're starting with includes a content placeholder or other placeholder that can contain an ole object, PPT is probably popping the newly created OLE object INTO that placeholder. Placeholders cannot be grouped with other shapes. If that turns out to be the problem, either start with a slide whose layout doesn't include a placeholder that can contain OLE objects, or delete the placeholder before creating your OLE object, or create the OLE object, duplicate it (giving you an OLE object that's not contained in a placeholder), then delete the original.
来源:https://stackoverflow.com/questions/11959849/powerpoint-c-sharp-add-in-shape-grouping-issue