用 Excel VBA 删除图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4480231/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:01:10  来源:igfitidea点击:

Deleting pictures with Excel VBA

vbaexcel-2007

提问by Arlen Beiler

How do I delete all the pictures in an Excel 2007 worksheet? A working code example would be great.

如何删除 Excel 2007 工作表中的所有图片?一个有效的代码示例会很棒。

回答by jony

The simplest way:

最简单的方法:

Activesheet.Pictures.Delete

or

或者

Activesheet.Shapes.Delete

Depending on the type of object your picture is.

根据您的图片的对象类型。

Deletes all pictures with greater efficiency then iterating (looping through) and deleting them one by one.

以更高的效率删除所有图片,然后迭代(循环)并一张一张地删除它们。

回答by Eric Fortis

Dim shape As Excel.shape

For Each shape In ActiveSheet.Shapes
        shape.Delete
Next

回答by Eneas Gesing

To delete all pictures or others shapes, you can iterate all of them and check the type:

要删除所有图片或其他形状,您可以迭代所有图片并检查类型:

Dim shape As Excel.shape

For Each shape In ActiveSheet.Shapes

    Select Case shape.Type
        Case msoPicture, msoMedia, msoShapeTypeMixed, msoOLEControlObject, msoAutoShape
            shape.Delete
        Case Else
            'Do nothing
    End Select
Next

In my case this code was usefull because my sheet was full of transparent shapes of type msoAutoShape which I thought were pictures. So, Activesheet.Pictures.Delete was not working.

就我而言,这段代码很有用,因为我的工作表充满了我认为是图片的 msoAutoShape 类型的透明形状。因此,Activesheet.Pictures.Delete 不起作用。

You can find all shape types on this link: http://msdn.microsoft.com/en-us/library/aa432678(v=office.12).aspx

您可以在此链接上找到所有形状类型:http: //msdn.microsoft.com/en-us/library/aa432678(v=office.12).aspx