One Central Header/Footer used by Multiple Docs (Word 2003 or 2007)
Inside Word (2003 or 2007), is there a way to have one Header/Footer that is used by Multiple documents?
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 mai开发者_StackOverflow社区n document and only have to change the main document?
If there is not a built in way, has anyone done this using VBA?
I'm not sure how will this will work in practice, but you can insert other files into a Word document as a link.
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.
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.
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).
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
精彩评论