Hi,

We have an issue with word whereby the server that held all the templates
and docs needed replacing, the new server (which has a different name to the
old one) works fine apart from one thing - whenever we open a word doc that
has a template set in the tools/templates and addins option it takes up to 5
minutes to open. We needed a way to change the template in the doc
automatically and set it to normal.dot. I found this Knowledgebase article
830561 that has this macro in it:

Sub Test()
Dim strFilePath As String

Dim strPath As String

Dim intCounter As Integer

Dim strFileName As String

Dim OldServer As String

Dim objDoc As Document

Dim objTemplate As Template

Dim dlgTemplate As Dialog

Dim nServer As Integer

'hardcode the name of the old server.

OldServer = "{enter the name of the Old Server}"

nServer = Len(OldServer)

strFilePath = InputBox("What is the folder location that you want to
use?")

If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"

strFileName = Dir(strFilePath & "*.doc")

Do While strFileName <> ""

Set objDoc = Documents.Open(strFilePath & strFileName)

Set objTemplate = objDoc.AttachedTemplate

Set dlgTemplate = Dialogs(wdDialogToolsTemplates)

strPath = dlgTemplate.Template

If LCase(Left(strPath, nServer)) = LCase(OldServer) Then

objDoc.AttachedTemplate = NormalTemplate

End If

strFileName = Dir()

objDoc.Save

objDoc.Close

Loop

Set objDoc = Nothing

Set objTemplate = Nothing

Set dlgTemplate = Nothing

End Sub
The above macro does what we need it to apart from one thing - we need it to
automatically go through all subfolders as well as we have hundreds of word
docs with this issue, how can I change the above macro to include sub
folders?

--

echo21

RE: change macro to include subfolders by davelett

davelett
Fri Feb 17 12:16:27 CST 2006

Hi Jim,

Have a look at the article "How to get the names of all the folders in the
folder tree, starting from a specified folder" at
http://word.mvps.org/faqs/macrosvba/ReadFoldersIntoArray.htm.

You can probably nest your routine inside this one.
Then, to access each file in that folder, have a look at the article "How to
read the filenames of all the files in a directory into an array" at
http://word.mvps.org/faqs/macrosvba/ReadFilesIntoArray.htm

HTH,
Dave

"Jim" wrote:

> Hi,
>
> We have an issue with word whereby the server that held all the templates
> and docs needed replacing, the new server (which has a different name to the
> old one) works fine apart from one thing - whenever we open a word doc that
> has a template set in the tools/templates and addins option it takes up to 5
> minutes to open. We needed a way to change the template in the doc
> automatically and set it to normal.dot. I found this Knowledgebase article
> 830561 that has this macro in it:
>
> Sub Test()
> Dim strFilePath As String
>
> Dim strPath As String
>
> Dim intCounter As Integer
>
> Dim strFileName As String
>
> Dim OldServer As String
>
> Dim objDoc As Document
>
> Dim objTemplate As Template
>
> Dim dlgTemplate As Dialog
>
> Dim nServer As Integer
>
> 'hardcode the name of the old server.
>
> OldServer = "{enter the name of the Old Server}"
>
> nServer = Len(OldServer)
>
> strFilePath = InputBox("What is the folder location that you want to
> use?")
>
> If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
>
> strFileName = Dir(strFilePath & "*.doc")
>
> Do While strFileName <> ""
>
> Set objDoc = Documents.Open(strFilePath & strFileName)
>
> Set objTemplate = objDoc.AttachedTemplate
>
> Set dlgTemplate = Dialogs(wdDialogToolsTemplates)
>
> strPath = dlgTemplate.Template
>
> If LCase(Left(strPath, nServer)) = LCase(OldServer) Then
>
> objDoc.AttachedTemplate = NormalTemplate
>
> End If
>
> strFileName = Dir()
>
> objDoc.Save
>
> objDoc.Close
>
> Loop
>
> Set objDoc = Nothing
>
> Set objTemplate = Nothing
>
> Set dlgTemplate = Nothing
>
> End Sub
> The above macro does what we need it to apart from one thing - we need it to
> automatically go through all subfolders as well as we have hundreds of word
> docs with this issue, how can I change the above macro to include sub
> folders?
>
> --
>
> echo21
>
>
>