I tried to delete all shapes with the following piece of codes:
PowerPoint.Application ppApp = Globals.ThisAddIn.Application; PowerPoint.Presentation ppP = ppApp.ActivePresentation; PowerPoint.Slide ppS = ppApp.ActiveWindow.Selection.SlideRange[1]; PowerPoint.Shapes shapes = ppS.Shapes; foreach (PowerPoint.Shape shape in shapes) { shape.Delete(shape); }
It cannot delete all shapes as I expected, because when a shape is deleted, it will affect the PowerPoint.Shapes. If I want to delete all shapes, I need to add all shapes into a List, and then delete each one like:
PowerPoint.Application ppApp = Globals.ThisAddIn.Application; PowerPoint.Presentation ppP = ppApp.ActivePresentation; PowerPoint.Slide ppS = ppApp.ActiveWindow.Selection.SlideRange[1]; PowerPoint.Shapes shapes = ppS.Shapes; if (shapes == null) return; List listShapes = new List(); foreach (PowerPoint.Shape shape in shapes) { listShapes.Add(shape); } foreach (PowerPoint.Shape shape in listShapes) { shape.Delete(); }
Is there any other ways to delete faster?
You can't iterate over a collection and delete an item from that collection, because that affects the collection. So just try avoiding the for-each loop:
while (shapes.Count > 0) {
shapes[0].Delete();
}
Not 100% sure of the syntax in relationship to PowerPoint, but that's the general idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.