VBA MS Word - 删除所有页眉而非页脚

问题描述 投票:0回答:1

我面临一个问题,我试图删除所有页眉,但我也删除页脚。我正在使用 WDHeaderFooterPrimary,但页脚仍然被删除。我已将所有部分移至页面末尾,以便将来处理该文档。

     ' remove all sections
    For i = doc.Sections.Count To 5 Step -1
          With doc.Sections(i).Range.Find
               .ClearFormatting
               .Text = "^b"
               .Replacement.ClearFormatting
               .Replacement.Text = ""
               .Execute Replace:=wdReplaceAll
          End With
    Next

     'insert section after each page
     For i = 5 To doc.ComputeStatistics(wdStatisticPages)
          Selection.GoTo What:=wdGoToPage, Which:=wdGoToLast, Count:=i
          Selection.InsertBreak Type:=wdSectionBreakContinuous
     Next
     
     ' delete shapes
     For i = 5 To doc.Sections.Count
          Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=i
          Set hdr = doc.Sections(i).Headers(wdHeaderFooterPrimary)
               For Each shp In hdr.Shapes
                    shp.Delete
               Next
     Next

如何仅删除标题?

vba ms-word header
1个回答
0
投票
Sub RemoveAllHeaders()
    Dim doc As Document
    Dim sec As Section
    Dim headerType As Variant
    Dim hdr As HeaderFooter
    Dim shp As Shape

    Set doc = ActiveDocument

    For Each sec In doc.Sections
        For Each headerType In Array(wdHeaderFooterPrimary, wdHeaderFooterFirstPage, wdHeaderFooterEvenPages)
            Set hdr = sec.Headers(headerType)
            If hdr.Exists Then
                hdr.Range.Text = ""
                
                For Each shp In hdr.Shapes
                    shp.Delete
                Next shp
            End If
        Next headerType
    Next sec
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.