问题描述
我有大约100个需要更改页眉和页脚的文档。
I have around 100 documents for which the header and footer need to be changed.
有没有可能只是通过编写vba代码或宏文件中的文件?
Is there a possibility that i can do it just by writing a vba code or Macro in a word file?
是否可以在宏中指定一个特定的文件夹,它将为页脚中的所有文档添加页眉和页脚?
Is it possible to give a specific folder in a macro which ll add the header and footer for all the documents in that footer?
以下代码给我
the below code gives me
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
推荐答案
在您提供的代码中,您尝试使用旧的 .FileSearch
属性。它曾经工作到MS Office 2003,但不是现在。代码改进了你。它会打开一个标准的文件窗口,您可以选择一个或几个文件来处理。
In the code you provided you have tried to use old .FileSearch
property. It used to work until MS Office 2003 but not now. Here goes code improved for you. It will open a standard file window where you can pick one or few files to process.
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
这篇关于为许多单词文档添加页眉和页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!