【问题标题】:To Add Header and Footer for many word documents?为许多word文档添加页眉和页脚?
【发布时间】:2013-03-09 06:14:46
【问题描述】:

我有大约 100 个文档需要更改页眉和页脚。

有没有可能我可以通过在 word 文件中编写 vba 代码或宏来做到这一点?

是否可以在宏中指定一个特定文件夹,为该页脚中的所有文档添加页眉和页脚?

下面的代码给了我

错误-5111

Private Sub Submit_Click()

        Call openAllfilesInALocation

End Sub


Sub openAllfilesInALocation()
Dim i As Integer
With Application.FileSearch
.NewSearch
.LookIn = "C:\MyFolder\MySubFolder"
.SearchSubFolders = False
.FileName = "*.xls"
.Execute
For i = 1 To .FoundFiles.Count
'Open each workbook
Set Doc = Documents.Open(FileName:=.FoundFiles(i))
'Perform the operation on the open workbook
'wb.Worksheets("sheet1").Range("A1") = Date
'Save and close the workbook
With ActiveDocument.Sections(1)
    .Headers(wdHeaderFooterPrimary).Range.Text = "Header goes here"
    .Footers(wdHeaderFooterPrimary).Range.Text = "Footer goes here"
End With

Doc.Save
Doc.Close
'On to the next workbook
Next i
End With
End Sub

【问题讨论】:

  • 对于您的所有问题,我的回答都是肯定的!如果您尝试任何事情并且需要任何帮助,您可以分享您的代码以获得任何支持
  • Kaz Jaw 请检查代码....我收到 5111 错误...

标签: vba header ms-word footer


【解决方案1】:

在您提供的代码中,您尝试使用旧的.FileSearch 属性。它曾经工作到 MS Office 2003,但现在不行。这是为您改进的代码。它将打开一个标准文件窗口,您可以在其中选择一个或几个文件进行处理。

Sub openAllfilesInALocation()
Dim Doc
Dim i As Integer

Dim docToOpen As FileDialog
Set docToOpen = Application.FileDialog(msoFileDialogFilePicker)
    docToOpen.Show

For i = 1 To docToOpen.SelectedItems.Count
'Open each document
Set Doc = Documents.Open(FileName:=docToOpen.SelectedItems(i))

With ActiveDocument.Sections(1)
    .Headers(wdHeaderFooterPrimary).Range.Text = "Header goes here"
    .Footers(wdHeaderFooterPrimary).Range.Text = "Footer goes here"
End With

Doc.Save
Doc.Close

Next i

End Sub

【讨论】:

猜你喜欢
  • 2021-07-21
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 2017-05-26
  • 2012-07-25
  • 2013-05-07
  • 2022-06-11
  • 2010-12-03
相关资源
最近更新 更多