Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Apply a template to all Word 2003 documents

Status
Not open for further replies.

icodian

IS-IT--Management
Aug 28, 2001
74
US
I recently created a header that I want to appear on all of the Word document handouts that we use. This adds up to approximately 200-250 Word docs. My header is currently a template and I am using it for any new docs I create. However, I would like to be able to apply the header to the existing 200 docs easily. Is anyone familiar with a way to do this?

Ideally, I'd like to make a change to the template and have it show up on every Word doc that is printed in the future.

I'm using Word 2003.

Thanks for any input!
 
Normal.dot is the template used when you bring up a new document. You can place your header on this and it will appear on all future documents. My is in C:\windows\application data\microsoft\templates

Just do a search on normal.dot to find where yours is placed.

 
This is a little tricky, but it can be done. It is VERY easy to attach a template to bunches of existing Word documents, using a loop with Dir. You loop through the Dir function, opening each document, and then use:
Code:
ActiveDocument.AttachedTemplate = "[i]path_to_template[/i]
Unfortunately, while this DOES attach the template to the existing document, it will NOT put in the existing header from the template. That ONLY happens when you use a template to create a NEW document.

However, you can trick things. What you can do is OPEN the template file itself, make a HeaderFooter object out of the proper header...THEN loop through the Dir function, opening each file, make a HeaderFooter object of its header, make that headerfooter = the HeaderFooter from the template. Save the current file, close it (destroying the current temp headerfooter object), and then go on to the next file in the Dir. Make a variation of the following using your own paths.
Code:
Sub CopyHeaderFromTemplate()
Dim myFile
Dim myDocPath As String
Dim myTemplatePath As String
Dim myTemplateHeader As HeaderFooter
Dim myDocHeader As HeaderFooter
[COLOR=red]' set variables for paths[/color red]
myDocPath = "c:\test\test2\"
myTemplatePath = "C:\Documents and Settings\Gerry\Application Data\" _
& "Microsoft\Templates\MyNew\makeheader.dot"
[COLOR=red]' set up the Dir function[/color red]
myFile = Dir(myPath & "*.doc")
[COLOR=red]' open the template file itself
' and make a HeaderFooter object
' NOTE!!! if you are NOT using the Primary headerfooter
' make sure you change this to the correct one![/color red]
Documents.Open FileName:= myTemplatePath
    Set myTemplateHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
[COLOR=red]' start the Dir loop[/color red]
Do While myFile <> ""
  Documents.Open FileName:=myPath & myFile
[COLOR=red]' attach the template, then make 
' header object out of the CURRENT document header[/color red]
  With ActiveDocument
    .AttachedTemplate = myTemplatePath
    Set myDocHeader = .Sections(1).Headers(wdHeaderFooterPrimary)
[COLOR=red]' now make the CURRENT document header object
' equal the TEMPLATE file header object[/color red]
    myDocHeader.Range = myTemplateHeader.Range
[COLOR=red]' destroy the header object
' and save / close the current doc[/color red]
    Set myDocHeader = Nothing
    .Save
    .Close
  End With
[COLOR=red]' go on to the next file in the Dir[/color red]
  myFile = Dir
Loop
[COLOR=red]' explicitly close the template file[/color red]
Documents("makeheader.dot").Close
End Sub

Gerry
 
Note to fneily:

Yes, that works if you want the header in ALL further (new) documents. However, that is most likely not what is desired. I believe the issue is getting content from a specific template (and NOT normal.dot) into existing documents....not new ones.

Note to icodian:
Ideally, I'd like to make a change to the template and have it show up on every Word doc that is printed in the future.
Unfortunately, again, things do not work quite that way. Changes to a template are NOT automatically refreshed in existing documents that are cloned from that template. It is fairly easy to add a simple instruction for existing documents to refresh styles that are changed in the attached template. However, it is NOT easy to refresh content. In fact (depending on exactly what the content changes are), it could be tricky...although I have no doubts it could be done.

This is why template files should be carefully, and thoughtfully designed. You want to minimize CONTENT changes.

Good luck. I hope the above post helps.

Gerry
 
Whoooops!!! I had changed the variable names and missed correctly the names in one spot! The line:
Code:
myFile = Dir(myPath & "*.doc")
should be
Code:
myFile = Dir(my[b][COLOR=red]Doc[/color red][/b]Path & "*.doc")

Apologies if anyone has a failure with that. But hey....you should be checking this crap anyway...[wavey2]

Gerry
 
Da Dah!!!!!!!! Although, I guess it depends on what is in the header that you want to get into the existing documents. A header creating macro may being more complicated than a header copying macro.

Although, Lilliabeth...my code IS a macro. Just kidding, I am sure you are quite aware of that. It does get the stuff from the template into existing documents. Granted, the fact that the header being copied is from a template file is actually, truthfully, irrelevant. It is simply copying the header from a file - that happens to be the newly attached one.

I was assuming that the template that is being attached has other things that are desired - macros, styles...whatever.

If that is not relevant...then yeah, there could be easier ways of doing it. I was simply trying to respond to what was stated.

Gerry
 
I should have clarified - I was asking the original poster why they specifically asked for a template.

 
Don't know.....kinda curious myself. I suspect...but of course could be wrong...the belief was that template content would be inherited by attaching.

Gerry
 
It would seem simpler to create a new document from the template, and insert an existing document into the new doc that would now have the headers from the template.

Save the result over the inserted file.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top