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

时间:2015-03-30 08:40:56

标签: vba ms-word word-vba

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

所以图表/表格/ excel表格/图像。

我已经尝试录制并操纵它但无济于事。

这适用于图像&图表,但不是表格/ 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

2 个答案:

答案 0 :(得分:2)

对于表格,请使用:

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

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

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

答案 1 :(得分:1)

此宏删除图表,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