工作中,尤其是代理公司的员工,经常会为Word文档添加页眉页脚,如果页眉页脚不想要了,一个文件删除还好,若是大量文件要删除,会是很头疼的,因为一个一个处理效率实在太低了,这里提供一种利用宏批量处理文档页眉页脚的方式,快来学习吧。
批量删除word文档的页眉页脚,可以用下面的vba程序来实现,需要注意的是,在批量操作前,请先做好备份。
操作步骤如下:
1、先把需要删除页眉页脚的多个word文档放在一个文件夹下,然后打开其中一个文档。
2、选择一个文档打开,在打开的文档中,选择在“视图”或“工具”(版本不同,打开方式有所区别)菜单中选择“宏”,点击打开“宏”
1/6
3、随后弹出“宏”对话框,在宏名中输入“Application”,然后点击"创建"按钮(这里我之前测试时已经创建,大家自己创建一个新的即可)。
2/6
随后弹出“Microsoftvisual basic”设计,在编辑处输入如下代码:
代码内容:
Dim Fdlg As FileDialog, Fl
Dim Fso, Fld, Fln, Wk
Set Fdlg = Application.FileDialog(msoFileDialogFolderPicker)
With Fdlg
.Title = "选择要处理目标文件夹" & "——(删除里面所有Word文档的页眉页脚)"
If .Show = -1 Then
MyPath = .SelectedItems(1)
Else
3/6
Exit Sub
End If
End With
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Fld = Fso.GetFolder(MyPath)
Set Fln = Fld.Files
For Each Wk In Fln
Set myDoc = Documents.Open(FileName:=Fld & "" & Wk.Name)
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.WholeStory
With Selection.ParagraphFormat
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth075pt
.DefaultBorderColor = wdColorAutomatic
End With
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
4/6
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
" 以上可以换成是你自己录制的宏
" C公共部分的代码
Application.DisplayAlerts = False "强制执行“是”
"ActiveDocument.Saved = True"强制执行“否”
ActiveDocument.Close "退出
Next
End Sub
4、输入代码后,关闭窗口返回文档,重新选择在“视图”或“工具”菜单中选择“宏”--“宏”,在弹出的宏名中选择"批量删除Word页眉页脚",最后点击“运行”---确认即可快速地删除多个Word页眉页脚。
5/6