批量删除Word2016的页眉页脚
(2018-05-13 23:10:59)
标签:
wordvba页眉页脚批量删除 |
分类: Microsoft技术 |
网上有 n 多的vba脚本说是可以批量删除页眉页脚。
.Title = "选择要处理目标文件夹"
& "—(删除里面所有Word文档的页眉页脚 *.doc, *.docx)"
If .Show = -1 Then
strFolder = .SelectedItems(1)
Else
Exit
Sub
End If
If strFolder & ""
& strFile <> wdDocSrc.FullName Then
Set wdDocTgt =
Documents.Open(FileName:=strFolder & "" & strFile, _
Format:=wdOpenFormatAuto, Revert:=False,
ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, Visible:=False)
With wdDocTgt
For Each
Sctn In .Sections
'For Headers
For Each HdFt In
Sctn.headers
HdFt.Range.Delete
HdFt.Range.Select
Selection.WholeStory
Selection.Borders(wdBorderTop).LineStyle =
wdLineStyleNone
Selection.Borders(wdBorderLeft).LineStyle =
wdLineStyleNone
Selection.Borders(wdBorderBottom).LineStyle =
wdLineStyleNone
Selection.Borders(wdBorderRight).LineStyle =
wdLineStyleNone
Next
'For footers
For Each HdFt In
Sctn.Footers
HdFt.Range.Delete
Next
Next
.Close
SaveChanges:=True
End With
End If
strFile = Dir()
但在word 2016中运行报错,查了之后说是新版本更新了语法。我这编程老白,经过多次实验,终于使用 word
2016实现批量删除页眉页脚(包括页眉中的横线),这里记录一下,有需要的可以使用。
首先打开word,找到“视图”-“宏”-“查看宏”,或者直接键盘
Alt+F8,宏名那里填上 RemoveHeadersAndFooters
或者其他名字,然后点创建,然后点编辑。然后将下面的内容粘进去,保存,运行。选择一个目录,目录里面的所有word文档会挨个处理(注意如果新建的宏包含了头和尾,可以删掉)。
Sub RemoveHeadersAndFooters()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
Dim wdDocTgt As document, wdDocSrc As document
Dim Sctn As Section, HdFt As HeaderFooter
'strFolder = GetFolder
With Application.FileDialog(msoFileDialogFolderPicker)
End With
If strFolder = "" Then Exit Sub
Set wdDocSrc = ActiveDocument
strFile = Dir(strFolder & "\*.doc?", vbNormal)
While strFile <> ""
Wend
Set wdDocSrc = Nothing: Set wdDocTgt = Nothing
Application.ScreenUpdating = True
MsgBox "所选Word文档的页眉页脚已删除!!!", 64, "批量处理完毕"
End Sub
后一篇:批量转换word文档为PDF

加载中…