Microsoft Word 宏删除文本以外的所有内容

Microsoft Word Macro to delete everything other than text

我正在尝试在 word 中创建一个宏,用于删除除文本以外的所有内容。

所以charts/tables/exceltables/images.

我试过录制一个并对其进行处理,但无济于事。

这适用于图像和图表,但不适用于 tables/excel 表格。

Sub deleteimages()
    Dim i As Integer

    With ActiveDocument
        For i = 1 To .InlineShapes.Count
            .InlineShapes(i).ConvertToShape
        Next i

        Dim Shp As Shape
        For Each Shp In ActiveDocument.Shapes
            If Shp.Type = msoTextBox Then Shp.Delete
        Next Shp

        For Each Shp In ActiveDocument.Shapes
            If Shp.Type = msoTable Then Shp.Delete
        Next Shp

        ActiveDocument.Shapes.SelectAll
        Selection.Delete
    End With
End Sub

对于表格,使用这个:

Sub deletetables()
    Dim i As Integer

    With ActiveDocument
        For i = .Tables.Count To 1 Step -1
            .Tables(i).Delete
        Next i
    End With
End Sub

图表和其他对象使用相同的逻辑。

更多信息,请参阅:Word Object Model Reference

顺便说一句:由于一系列原因,我建议从最后一个开始删除对象。另一种方法是使用 Do While... loop:

Do While ActiveDocument.Tables.Count>1
    ActiveDocument.Tables(1).Delete
Loop

此宏删除图表、MS 表格、Excel 复制的表格和图像。

Sub deleteNoise()
Dim objPic As InlineShape
For Each objPic In ActiveDocument.InlineShapes
objPic.Delete
Next objPic
    Dim tbl As Table
    For Each tbl In ActiveDocument.Tables
        tbl.Delete
    Next tbl
        Dim shp As Shape
ActiveDocument.Shapes.SelectAll
        Selection.Delete
End Sub