Image 使用VBA宏删除PowerPoint中的图片
我正在使用以下VBA宏删除PowerPoint幻灯片中的所有图片:Image 使用VBA宏删除PowerPoint中的图片,image,vba,powerpoint,Image,Vba,Powerpoint,我正在使用以下VBA宏删除PowerPoint幻灯片中的所有图片: Public Function delete_slide_object(slide_no) ' Reference existing instance of PowerPoint Set PPApp = GetObject(, "Powerpoint.Application") ' Reference active presentation Set PPPres = PPApp.ActiveP
Public Function delete_slide_object(slide_no)
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Delete object in slide
Set PPSlide = PPPres.Slides(slide_no)
For Each PPShape In PPSlide.Shapes
If PPShape.Type = msoPicture Then
PPShape.Delete
End If
Next PPShape
Set PPShape = Nothing
Set PPSlide = Nothing
Set PPPres = Nothing
End Function
此代码正在删除部分但不是全部图片。运行此代码3次后,所有图片都将被删除。我哪里做错了?请告诉我从集合中删除项目时,您必须使用不同的迭代 试试这个:
Dim p as Long
For p = PPSlide.Shapes.Count to 1 Step -1
Set PPShape = PPSlide.Shapes(p)
If PPShape.Type = msoPicture Then PPShape.Delete
Next
这是因为删除项目时集合会重新编制索引,因此如果删除
Shapes(2)
,则先前的Shapes(3)
在删除后会变成Shapes(2)
,并被循环有效地“跳过”。为了避免这种情况,您必须从最后一个形状开始,并以相反的顺序删除它们。他们有机会尝试答案吗?嗨!刚刚试过密码。工作得很好!非常感谢!:)