场景
多个word都需要加页眉,可以批量操作。
建议先备份文档,避免操作异常引起文档内容损坏或丢失。
操作步骤
在word中就可以进行批量改页眉页脚。
-
打开Microsoft Word。
-
按下
Alt
+F11
组合键,这将打开Microsoft Visual Basic for Applications(VBA)编辑器。 -
在VBA编辑器中,点击“插入”菜单,然后选择“模块”。
-
将如下代码贴在窗口中。
Sub ChangeHeaders() Dim strFolder As String, strFile As String Dim doc As Document ' 获取文件夹路径 strFolder = "C:\your\folder\path\" strFile = Dir(strFolder & "*.docx") ' 遍历文件夹中的所有.docx文件 Do While strFile <> "" Set doc = Documents.Open(strFolder & strFile) ' 更改页眉 With doc.Sections(1).Headers(wdHeaderFooterPrimary) .Range.Text = "新的页眉内容" For Each para In .Range.Paragraphs