Search code examples
vbams-wordlineshapes

Word VBA: How to Delete Shapes (horizontal line)?


Trying to find all horizontal lines in a Word document and delete them. Having some trouble... any help is appreciated. Code below.

Sub Replace()

Dim oShp As Shape
Dim i As Long

With ActiveDocument
    For i = .InlineShapes.Count To 1 Step -1
        With .InlineShapes(i)
            oShp.Select
            Selection.Delete
        End With
    Next
End With

End Sub

Solution

  • You could just iterate the inlineShapes collection and delete only the shapes whose type is an horizontal line.

    Public Sub DeleteHorizontalLines()
    
        Dim docShape As InlineShape
        
        For Each docShape In ActiveDocument.InlineShapes
        
            If docShape.Type = wdInlineShapeHorizontalLine Then
                docShape.Delete
            End If
            
        Next docShape
    
    End Sub
    

    Let me know if it works

    EDIT: To replace the shape with a text, you could add the text before deletin it.

    Add this line:

    docShape.Range.InsertAfter "Horizontal line"
    

    Before this line:

    docShape.Delete