使用VBA宏在PowerPoint中删除图片 [英] Delete pictures in PowerPoint using VBA macro

查看:177
本文介绍了使用VBA宏在PowerPoint中删除图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下VBA宏删除PowerPoint幻灯片中的所有图片:

I am using the following VBA Macro to delete all the pictures in a PowerPoint slide:

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次后,所有图片将被删除.我要去哪里错了?请让我知道

This code is deleting some but not all the pictures.After running this code 3 times , all the pictures get deleted. Where am i going wrong? Kindly let me know

推荐答案

从集合中删除项目时,必须使用其他迭代.

When deleting items from a collection, you have to use a different iteration.

尝试一下:

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)删除后,实际上被循环跳过"了.为避免这种情况,您必须从 last 形状开始,然后以相反的顺序删除它们.

This is because the collection is re-indexed when items are removed, so if you delete Shapes(2) then what was previously Shapes(3) becomes Shapes(2) after the deletion, and is effectively "skipped" by the loop. To avoid this, you have to start with the last shape, and delete them in reverse order.

这篇关于使用VBA宏在PowerPoint中删除图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆