Tuesday, December 24, 2024

VBA Word - Delete images from document with specific URL

The following VBA code deletes images from document with specific URL:
Sub DeleteImagesWithSpecificURL()
    Dim iShape As InlineShape
    Dim shp As Shape
    Dim doc As Document
    Dim searchURL As String
    
    searchURL = "https://hoven.in/aspnet-core/asp-net-core-course-buy.html"
    
    Set doc = ActiveDocument
    
    ' Check InlineShapes (images in the text flow)
    For Each iShape In doc.InlineShapes
        If iShape.Type = wdInlineShapePicture Then
            If InStr(1, iShape.AlternativeText, searchURL, vbTextCompare) > 0 Then
                iShape.Delete
            End If
        End If
    Next iShape
    
    ' Check Shapes (floating images)
    For Each shp In doc.Shapes
        If shp.Type = msoPicture Then
            If InStr(1, shp.AlternativeText, searchURL, vbTextCompare) > 0 Then
                shp.Delete
            End If
        End If
    Next shp
End Sub

No comments:

Post a Comment

Hot Topics