Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

Macro to change Document Template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Richard Conway - 29 Feb 2004 13:31 GMT
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.
Howard Kaikow - 29 Feb 2004 15:46 GMT
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

Signature

http://www.standards.com/; See Howard Kaikow's web site.

> 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
[quoted text clipped - 53 lines]
> Thanks in advance,
> Ric.
Ric - 05 Mar 2004 11:20 GMT
How do I do a non-visable instance of word?

> Try the following, it's likely faster.
>
[quoted text clipped - 87 lines]
> > Thanks in advance,
> > Ric.
Howard Kaikow - 05 Mar 2004 14:53 GMT
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.

Signature

http://www.standards.com/; See Howard Kaikow's web site.

> How do I do a non-visable instance of word?
>
[quoted text clipped - 91 lines]
> > > Thanks in advance,
> > > Ric.
Peter Hewett - 05 Mar 2004 15:02 GMT
Hi Ric

Fopr your answer see your other post too this NG.

Cheers - Peter

"Ric" <ric@somewhere.com> wrote in news:edEExPqAEHA.712
@tk2msftngp13.phx.gbl:

> How do I do a non-visable instance of word?
>
[quoted text clipped - 91 lines]
>> > Thanks in advance,
>> > Ric.
jaysfive - 14 Dec 2004 08:27 GMT
> Hi Ric
>
[quoted text clipped - 106 lines]
> >> > Thanks in advance,
> >> > Ric.
jaysfive - 14 Dec 2004 08:31 GMT
> Hi Ric
>
[quoted text clipped - 106 lines]
> >> > Thanks in advance,
> >> > Ric.

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
Jelly - 04 Feb 2005 12:29 GMT
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

> > Hi Ric
> >
[quoted text clipped - 119 lines]
> ..LookIn = "G:\a_a_a"
> but it just starts at the first file
jaysfive - 14 Dec 2004 11:57 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.