Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 删除PowerPoint幻灯片的所有形状_.net_C# 4.0_Powerpoint_Powerpoint 2010 - Fatal编程技术网

.net 删除PowerPoint幻灯片的所有形状

.net 删除PowerPoint幻灯片的所有形状,.net,c#-4.0,powerpoint,powerpoint-2010,.net,C# 4.0,Powerpoint,Powerpoint 2010,我尝试使用以下代码删除所有形状: 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.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); } PowerPoint.Application ppApp=Globals.ThisAddIn.Application; PowerPoint.Presentation ppP=ppApp.ActivePresentation; PowerPoint.Slide ppS=ppApp.ActiveWindow.Selection.SlideRange[1]; PowerPoint.Shapes=ppS.Shapes; foreach(PowerPoint.Shape-in-shapes) { 形状。删除(形状); } 它不能像我预期的那样删除所有形状,因为删除形状时,它会影响PowerPoint.shapes。如果要删除所有形状,我需要将所有形状添加到列表中,然后删除每个形状,如:

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(); } PowerPoint.Application ppApp=Globals.ThisAddIn.Application; PowerPoint.Presentation ppP=ppApp.ActivePresentation; PowerPoint.Slide ppS=ppApp.ActiveWindow.Selection.SlideRange[1]; PowerPoint.Shapes=ppS.Shapes; 如果(shapes==null)返回; List listShapes=新列表(); foreach(PowerPoint.Shape-in-shapes) { listShapes.Add(shape); } foreach(listShapes中的PowerPoint.Shape) { shape.Delete(); }
还有其他更快的删除方法吗?

您不能遍历集合并从该集合中删除项目,因为这会影响集合。因此,只需尝试避免for each循环:

while (shapes.Count > 0) {
  shapes[0].Delete();
}

无法100%确定与PowerPoint相关的语法,但这是总体思路。

在VBA:Presentation.Slides(x)中,Shapes.Range.Delete将删除幻灯片x上的所有形状。@SteveRindsberg听起来是更好的答案。我很少冒险使用VBA。我们很清楚,你不必使用VBA,只要把我的建议翻译成你正在使用的任何东西就行了。我想这就是你理解它的方式,但我想确定。