Delete pictures by name

Bomba
3StarLounger
Posts: 281
Joined: 20 Jan 2019, 19:43

Delete pictures by name

Post by Bomba »

Master Hans,
Yesterday you gave this code (below) so that all pictures will be deleted, except sheet "DASH". Is it possible to modify this code so that it will delete all the pictures named picture 1?

Code: Select all

Sub DeleteAllObjectsInFolder()
    Const strFolder = "C:\...\2. February 2019\"
    Dim strFile As String
    Dim wbk As Workbook
    Dim wsh As Worksheet
    Dim i As Long
    Dim f As Boolean
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    strFile = Dir(strFolder & "*.xls*")
    Do While strFile <> ""
        Set wbk = Workbooks.Open(Filename:=strFolder & strFile, UpdateLinks:=False)
        For Each wsh In wbk.Worksheets
            If UCase(wsh.Name) <> "DASH" Then
                f = wsh.ProtectContents
                If f Then
                    wsh.Unprotect
                End If
                For i = wsh.Shapes.Count To 1 Step -1
                    wsh.Shapes(i).Delete
                Next i
                If f Then
                    wsh.Protect
                End If
            End If
        Next wsh
        wbk.Close SaveChanges:=True
        strFile = Dir
    Loop
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Thanks

User avatar
HansV
Administrator
Posts: 78241
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete pictures by name

Post by HansV »

Replace the loop

Code: Select all

                For i = wsh.Shapes.Count To 1 Step -1
                    wsh.Shapes(i).Delete
                Next i
with

Code: Select all

                On Error Resume Next
                wsh.Shapes("Picture 1").Delete
                On Error GoTo 0
Best wishes,
Hans

Bomba
3StarLounger
Posts: 281
Joined: 20 Jan 2019, 19:43

Re: Delete pictures by name

Post by Bomba »

Hi Master,
Nice job, thanks