Reply
 
Thread Tools Display Modes
  #1   Report Post  
Barry Millman
 
Posts: n/a
Default VBA to Copy Template styles to many documents...is this OK?

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

  #2   Report Post  
Cindy M -WordMVP-
 
Posts: n/a
Default

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 :-)

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't copy Autotext to new template Anita Microsoft Word Help 1 January 14th 05 10:02 PM
word xp crashes after macros are recorded kharris0405 Microsoft Word Help 3 January 11th 05 11:50 PM
NEED TO COPY A TEMPLATE TO SECOND PAGE IN THE SAME DOCUMENT TYSON Microsoft Word Help 2 January 11th 05 11:44 PM
Deleting template affects styles, specifically font size. BrownEyedMaria Microsoft Word Help 2 January 6th 05 07:59 PM
Continuous breaks convert to next page breaks Jennifer Hunt Microsoft Word Help 2 December 30th 04 06:45 PM


All times are GMT +1. The time now is 10:41 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"