1

可以使用 delete 命令删除页眉和页脚。但我想从页眉/页脚中删除除形状之外的所有内容。有两个范围:TextRange 和 ShapesRange。ShapeRange 可以按如下方式访问。

    For Each sec In worddoc.Sections
        For Each hdr In sec.Headers
            For Each sh In hdr.Shapes
                If sh.Left > 200 Then
                    'Do something
                End If
            Next sh
        Next hdr
    Next sec

如何删除文本范围?

通过设置 .TextRange="" 将删除表格,文本框?

4

1 回答 1

2

这是你想要的吗?

Option Explicit

Sub Sample()
    Dim ctl As ContentControl
    Dim tbl As Table
    Dim i As Long

    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range

        On Error Resume Next
        '~~> Delete all controls like textbox label etc
        For Each ctl In .ContentControls
            ctl.Delete
        Next

        '~~> Delete all tables
        For Each tbl In .Tables
            tbl.Delete
        Next
        On Error GoTo 0

        '~~> Delete all printable/non printable characters
        For i = 0 To 255
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = Chr(i)
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Next i
    End With

    MsgBox "Done" 

End Sub

快照

在此处输入图像描述

于 2012-06-05T01:40:33.003 回答