Word VBA: 如何删除图形(横线)?

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

正在尝试查找 Word 文档中的所有水平线并将其删除。遇到一些麻烦...任何帮助表示赞赏。下面的代码。

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

您可以迭代 inlineShapes 集合并仅删除类型为水平线的形状。

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

如果有效请告诉我

编辑:要用文本替换形状,您可以在删除之前添加文本。

添加这一行:

docShape.Range.InsertAfter "Horizontal line"

这一行之前:

docShape.Delete