views:

273

answers:

2

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 main document and only have to change the main document?

If there is not a built in way, has anyone done this using VBA?

+1  A: 

Hi Gerhard, 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
Oorang
+1  A: 

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.

Tom Winter
Thank you very much. This will work. One extra note, in Word 2007 the Insert | File location changed. It is now located on the Insert tab, in the Text group, and is called Object. Clicking this will open the Object dialog box where you select the “Create from file” tab and enter the .docx file you want to link too. It works very well. The next issue I see is I want to share these documents will three other people and the link has a full path and I would like it to be a relative path.
Gerhard Weiss
I doubt you can do anything about that. Word likes to store paths using full UNC.
Tom Winter