vba 多个文档使用的一个中央页眉/页脚(Word 2003 或 2007)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1940707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
One Central Header/Footer used by Multiple Docs (Word 2003 or 2007)
提问by Gerhard Weiss
Inside Word (2003 or 2007), is there a way to have one Header/Footer that is used by Multipledocuments?
在 Word(2003 或 2007)中,有没有办法让多个文档使用一个页眉/页脚?
I want to be able to change the header/footer in one spot and have it affect multiple documents.
我希望能够在一处更改页眉/页脚并使其影响多个文档。
i.e. I have 50 documents and they all have the same header/footer. Instead of opening all 50 documents to make the change, is there a way to link (OLE?) the 50 documents to a main document and only have to change the main document?
即我有 50 个文档,它们都有相同的页眉/页脚。不是打开所有 50 个文档进行更改,有没有办法将 50 个文档链接(OLE?)到一个主文档并且只需要更改主文档?
If there is not a built in way, has anyone done this using VBA?
如果没有内置方式,有没有人使用 VBA 做到这一点?
回答by Tom Winter
I'm not sure how will this will work in practice, but you can insert other files into a Word document as a link.
我不确定这在实践中将如何工作,但您可以将其他文件作为链接插入到 Word 文档中。
First create the document with the header/footer content, with the content in the body of the document. Save it.
首先创建带有页眉/页脚内容的文档,内容在文档正文中。保存。
Then go to one of your 50 documents, go into the header/footer. Go to INSERT | FILE. Locate the first file, then click the little drop-down arrow next to the OPEN button in the Insert File dialog. From the drop-down, select INSERT AS LINK. The content should now show up in the document. If you click in the content, normally it will have a grey background, to indicate it's really a Word field.
然后转到您的 50 个文档之一,进入页眉/页脚。转到插入 | 文件。找到第一个文件,然后单击“插入文件”对话框中“打开”按钮旁边的小下拉箭头。从下拉列表中,选择插入为链接。内容现在应该显示在文档中。如果您单击内容,通常它会有一个灰色背景,以表明它确实是一个 Word 字段。
Now when you change the first document, you can open the second document, update the field (click anywhere in it and hit F9) and the new content will be pulled in. You can also update fields programmatically pretty easy, or under TOOLS | OPTIONS | PRINT, there's a box to auto update the fields every time the document is printed.
现在,当您更改第一个文档时,您可以打开第二个文档,更新字段(单击其中的任意位置并按 F9),新内容将被拉入。您还可以非常简单地以编程方式更新字段,或者在 TOOLS | 下更新字段。选项 | PRINT,每次打印文档时都有一个自动更新字段的框。
回答by Oorang
AFAIK to alter a documents header (simply) must be done by having the document open. That said you have a few options. First if the documents are saved in the office XML format then you could open the files using the MSXML library and alter the data in the header. (Or any of the dozens of other ways to alter what is essentially a text file.) If the file(s) are still in the binary format you really only have one of two options. The first is to open the file via vba and alter the header via the document object model. The second would be to figure out the binary format (which is documented) and alter it using the VB6/VBA native binary IO (very non-trivial).
AFAIK 更改文档标题(简单地)必须通过打开文档来完成。也就是说你有几个选择。首先,如果文档以 office XML 格式保存,那么您可以使用 MSXML 库打开文件并更改标题中的数据。(或者改变本质上是文本文件的数十种其他方法中的任何一种。)如果文件仍然是二进制格式,您实际上只有两种选择之一。第一种是通过 vba 打开文件并通过文档对象模型更改标题。第二种是找出二进制格式(已记录)并使用 VB6/VBA 本机二进制 IO(非常重要)对其进行更改。
Unless I thought I could gain more time then I was going to lose writing code to alter the documents directly I would probably just loop through all the file in the folder, open them and alter them. As for storing the header somewhere... You could just put the header data in a text file and pull it in. Or keep a document template somewhere.
除非我认为我可以获得更多时间,否则我将失去编写直接更改文档的代码的时间,否则我可能只会遍历文件夹中的所有文件,打开它们并更改它们。至于将标题存储在某处...您可以将标题数据放在一个文本文件中并将其拉入。或者在某处保留一个文档模板。
Here is a very trivial example:
这是一个非常简单的例子:
Public Sub Example()
Dim asFiles() As String
Dim lFile As Long
Dim docCrnt As Word.Document
asFiles = GetFiles("C:\Test\", "*.doc")
For lFile = 0& To UBound(asFiles)
Set docCrnt = Word.Documents.Open(asFiles(lFile))
docCrnt.Windows(1).View.SeekView = wdSeekCurrentPageHeader
Selection.Text = "I am the header."
docCrnt.Close True
Next
End Sub
Public Function GetFiles( _
ByVal folderPath As String, _
Optional ByVal pattern As String = vbNullString _
) As String()
Dim sFile As String
Dim sFolder As String
Dim asRtnVal() As String
Dim lIndx As Long
If Right$(folderPath, 1&) = "\" Then
sFolder = folderPath
Else
sFolder = folderPath & "\"
End If
sFile = Dir(sFolder & pattern)
Do While LenB(sFile)
ReDim Preserve asRtnVal(lIndx) As String
asRtnVal(lIndx) = sFolder & sFile
lIndx = lIndx + 1&
sFile = Dir
Loop
If lIndx = 0& Then
ReDim asRtnVal(-1& To -1&) As String
End If
GetFiles = asRtnVal
Erase asRtnVal
End Function