除了shaperange之外,如何删除页眉和页脚

时间:2012-05-13 15:42:07

标签: vba header word-vba shapes

可以使用delete命令删除页眉和页脚。但我想从页眉/页脚中删除除Shapes之外的所有内容。有两个范围: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?

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

1 个答案:

答案 0 :(得分: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

<强>快照

enter image description here