vba 删除 PowerPoint 幻灯片上的所有形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24187347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting all shapes on a PowerPoint slide
提问by Attaque
I'm trying to delete all slides on a PowerPoint shape with VBA. What i'm trying to do is simply:
我正在尝试使用 VBA 删除 PowerPoint 形状上的所有幻灯片。我想要做的很简单:
For Each sld In ActivePresentation.Slides
For Each sh In sld.Shapes
sh.Delete
Next
Next
But this does not delete all shapes as illustrated in the image below: (The grey arrow is a shape i'm adding before running the code below, but doesn't dissapear either)
但这不会删除所有形状,如下图所示:(灰色箭头是我在运行下面的代码之前添加的形状,但也不会消失)
Any idea on what i'm doing wrong?
知道我做错了什么吗?
回答by Sean W.
That truly is interesting and I was able to re-create the problem. I also first tried to take a count and iterate through the count; however, the problem is that how many shapes are on there refreshes real-time. What I mean is if it counted the shapes at one point in time as 2 shapes, then when a shape is deleted there are only 1 shape on there. Therefore your best bet is to loop through the count of shapes deleting the first shape each time as such.
这真的很有趣,我能够重新创建问题。我也首先尝试进行计数并遍历计数;然而,问题是有多少形状会实时刷新。我的意思是,如果它在一个时间点将形状计算为 2 个形状,那么当删除一个形状时,那里只有 1 个形状。因此,您最好的办法是遍历形状计数,每次删除第一个形状。
For Each sld In ActivePresentation.Slides
TotalShapes = sld.Shapes.Count
For i = TotalShapes to 1 step -1
sld.Shapes(i).Delete
Next
Next
Thanks, Sean W.
谢谢,肖恩W。
回答by John Wilson
Or you can just say:
或者你可以说:
sld.Shapes.Range.Delete
回答by MWR
typo: missing 0 in > 0
While ActivePresentation.Slides.Count > 0
While ActivePresentation.Slides(1).Shapes.Count **> 0**
ActivePresentation.Slides(1).Shapes(1).Delete
Wend
ActivePresentation.Slides(1).Delete
Wend
回答by Steve Rindsberg
For Each sld In ActivePresentation.Slides
For x = sld.shapes.count to 1 step -1
sld.shapes(x).Delete
Next
Next
回答by Julius
I think, the easiest way is:
我认为,最简单的方法是:
while ActivePresentation.Slides.count>0
while ActivePresentation.Slides(1).shapes.count>
ActivePresentation.Slides(1).shapes(1).delete
wend
ActivePresentation.Slides(1).delete
wend