World中利用宏命令批量删除页眉和页脚(亲测好用!)

Sub 批量删除页眉页脚()
'
' 批量删除页眉页脚 宏
'
'
Dim myDialog As FileDialog, oDoc As Document, oSec As Section
    Dim oFile As Variant, myRange As Range
    On Error Resume Next
    '定义一个文件夹选取对话框
    Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
    With myDialog
        .Filters.Clear    '清除所有文件筛选器中的项目
        .Filters.Add "所有 WORD 文件", "*.docx", 1    '增加筛选器的项目为所有WORD文件
        .AllowMultiSelect = True    '允许多项选择
        If .Show = -1 Then    '确定
            For Each oFile In .SelectedItems    '在所有选取项目中循环
                Set oDoc = Word.Documents.Open(FileName:=oFile, Visible:=False)
                For Each oSec In oDoc.Sections    '文档的节中循环
                    Set myRange = oSec.Headers(wdHeaderFooterPrimary).Range
                    myRange.Delete    '删除页眉中的内容
                    myRange.ParagraphFormat.Borders(wdBorderBottom).LineStyle = wdLineStyleNone    '段落下边框线
                    Set myRange = oSec.Footers(wdHeaderFooterPrimary).Range
                    myRange.Delete    '删除页脚中的内容
                Next
                oDoc.Close True
            Next
        End If
    End With
    MsgBox "所选Word文档的页眉页脚已删除!!!", 64, "批量处理完毕"
End Sub

    原文作者:最懂编程的医生
    原文地址: https://blog.csdn.net/weixin_40901505/article/details/116228299
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞