Macro to change Document Template

R

Richard Conway

Hi there, I need some help writing a macro that will change the Document
Template. The problem is that we have around 60,000 word documents that all
have document templates to which the path points to a server that no longer
exists. As a consequence every time one of the files is opened, Word hangs
for a while until it figures out it can't find the template, and then opens
the file. This is very annoying as you can imagine! I have written the
following macro:

Sub ChangeTemplateToNormal()

Dim i As Long

Dim strSaveFile As String

With Application.FileSearch

.FileName = "*.doc"

.LookIn = "<path to folder>"

.SearchSubFolders = False

.Execute

If .FoundFiles.Count > 0 Then

For i = 1 To .FoundFiles.Count

Documents.Open FileName:=.FoundFiles(i)

With ActiveDocument

.UpdateStylesOnOpen = False

.AttachedTemplate = "normal"

.Save

.Close

End With

Next i

End If

End With



End Sub

Which works fine up until a point but because of the shear number of files
involved it tends to crash frequently. Does anybody have any recomendations
of how I could add some error trapping into the script so it will carry when
it encounters an error?

Thanks in advance,
Ric.
 
H

Howard Kaikow

Try the following, it's likely faster.

Another thought is to use a non-visible instance of Word so the overhead of
opening and closing will be reduced.

You also have to verify that the files are not read-only.

Sub ChangeTemplateToNormal()
Dim i As Long
Dim varFile As Variant

With Application.FileSearch
.FileName = "*.doc"
.LookIn = "<path to folder>"
.SearchSubFolders = False
.Execute
For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
.UpdateStylesOnOpen = False
.AttachedTemplate = "normal"
.Save
.Close savechanges:=wdSaveChanges
End With
Next varFile
End With
End Sub
 
R

Ric

How do I do a non-visable instance of word?


Howard Kaikow said:
Try the following, it's likely faster.

Another thought is to use a non-visible instance of Word so the overhead of
opening and closing will be reduced.

You also have to verify that the files are not read-only.

Sub ChangeTemplateToNormal()
Dim i As Long
Dim varFile As Variant

With Application.FileSearch
.FileName = "*.doc"
.LookIn = "<path to folder>"
.SearchSubFolders = False
.Execute
For Each varFile In .FoundFiles
With Documents.Open(FileName:=varFile,
ConfirmConversions:=False)
.UpdateStylesOnOpen = False
.AttachedTemplate = "normal"
.Save
.Close savechanges:=wdSaveChanges
End With
Next varFile
End With
End Sub
 
H

Howard Kaikow

You would need to create a Word object from within your macro, then do all
operations on the documents thru that instance of Word, instead of the open
instance of Word.
 
J

jaysfive

Peter said:
Hi Ric

Fopr your answer see your other post too this NG.

Cheers - Peter

documents written will

How would I start the macro at a selected point in the file list. I've
tried .FileName = "r*.doc"
..LookIn = "G:\a_a_a"
but it just starts at the first file
 
J

jaysfive

How would I select only part of the file list. I tried With
Application.FileSearch
..FileName = "R*.doc"
..LookIn = "G:\a_a_MTC"
but it still started from the beginning of the file list
 
J

Jelly

Hi,

I have come across your post after looking for a good solution to this
problem.
I have run your macro on some files and works great although as there is soo
many files - talking thousands here - it is impossible for me to spend days
watching a computer screen just to get these files fixed.

My problem is that if the macro comes across a read only file it will stop
working and end its process. Thus meaning I have to change the file to a
non-read only file and then run the macro again.

Is there something I could put into the macro to prevent this from happening.

Also, when the macro gets to a file that wasn't closed/opened properly the
last time it waits for me to say yes or no to open the file (or something
like this). Again any solution to prevent this.

Any help is much appreciated.

Jelly
 

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