在我的某些工作表中,我看到了图片,Excel 显示的名称类似于“Grafik 4”。现在我想自动删除它们。我有 200 个 Excel 文件,总是包含相同的图片和相同的图片名称。我尝试过这样的方法:
Sub Clear_Images()
Dim directory As String, fileName As String ', sheet As Worksheet, i As Integer, j As Integer
Dim wks As Worksheet
Dim myPict As Shape
Application.ScreenUpdating = False
directory = "C:\Users\"
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
Workbooks.Open (directory & fileName)
For Each wks In ActiveWorkbook.Worksheets
For Each myPict In wks.Shapes
If myPict.Name = "Grafik 4" Then
myPict.Delete
End If
Next myPict
Next wks
Set wks = Nothing
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
End Sub
但在 VBA 中,每张图片似乎都有名称“Object x”(x 是从 1 到 open 的数字)。有人知道我如何才能读出图片的真实名称吗?
最好的,弗朗兹
答案1
我在 Excel 2013(德语)上做了一个快速测试。我插入了一些随机图片。它们自动获得了像你这样的名称,即“Grafik 4”
要删除这个形状,我将使用这种技术:
Sub test()
On Error Resume Next
Set image = ActiveSheet.Shapes("Grafik 4")
On Error GoTo 0
Debug.Print image.Name
If Not image Is Nothing Then
image.Delete
End If
End Sub
ExcelShapes("Grafik 4")
可以识别它的内部名称Picture 4
,您可以使用Debug.Print image.Name