VBA to Copy Template styles to many documents...is this OK?

B

Barry Millman

Hi:

I have a collection of documents (*.doc files) in a directory, and I
need them to all have the same style definitions. Unfortunately, my
template changes over time, and I needed a macro (VBA code) to copy the
styles from my updated template to all the documents. I did this in MS
Word 2000, running in the MS Windows XP (Professional) environment.

How does this look? It seems to work OK, except that it flashes the
open document on the screen briefly (but I can live with that).

-------------- start of the macro --------------------------------------
Sub CopyTemplate()
'
' CopyTemplate Macro
' Macro created 2005-01-16 by Barry Millman
'
' Gets all the .doc files in the directory (C:\temp)and applies the
' desired template to it (actually
' copies all the styles, and updates the ones with the same name).
' The template is:
' C:\Documents and Settings\Barry Millman\Application
' Data\Microsoft\Templates\CourseTemp1.dot
'
With Application.FileSearch
.FileName = "*.doc"
.LookIn = "C:\temp"
.Execute
For I = 1 To .FoundFiles.Count
myFile$ = .FoundFiles(I)
'
Documents.Open FileName:=myFile$ ' open the file
'
' now copy the styles from template
' Template:=" ....." all on one line
'
Documents(myFile$).CopyStylesFromTemplate _
Template:="C:\Documents and Settings\Barry Millman\Application
Data\Microsoft\Templates\CourseTemp1.dot"
'
Documents(myFile$).Close SaveChanges:=wdSaveChanges ' close & save
'
Next I
End With

End Sub

----------------- end of macro -----------------------------

Is this a relatively efficient (it does work!) and bugproof way to do
this or am I missing something major?

Thanks,
Barry
 
C

Cindy M -WordMVP-

Hi Barry,

You should ask this in one of the word.vba newsgroups :)

From a quick look, I'd make one change, for sure. I'd declare an object
variable to hold the document object you're opening, rather than
repeatedly using the file name to identify the document. It's more
reliable and should execute more quickly:

Dim doc as Word.Document

Set doc = Documents.Open Name:=myFile$
doc.CopyStylesFromTemplate 'etc.
doc.Close 'etc.
I have a collection of documents (*.doc files) in a directory, and I
need them to all have the same style definitions. Unfortunately, my
template changes over time, and I needed a macro (VBA code) to copy the
styles from my updated template to all the documents. I did this in MS
Word 2000, running in the MS Windows XP (Professional) environment.

How does this look?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top