Word VBA Script to modify all documents in folder & all subfolders

Hi - I received help on this forum a while ago when  someone provided code to open all templates in a specified folder and make a change, save changes, close then process the next item.  I've pasted that code below but what I now need it for the script to process the starting folder then and sub folders, and subfolder within those etc etc.  Any help on this would be greatly appreciated.

Sub templates()
' Display the names in C:\ that represent directories.
MyPath = "c:\templates\"    ' Set the path.
MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
Do While MyName <> ""    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Then
        ' Use bitwise comparison to make sure Document is a template
        If Right(MyName, 4) = ".dot" Then
            Documents.Open MyPath & MyName
            'Do things to template here*****************
            DoEvents
            ActiveDocument.Close wdSaveChanges
        End If    ' it represents a directory.
    End If
    MyName = Dir    ' Get next entry.
Loop

End Sub

Thanks

Rob
LVL 9
robjeevesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:

Sub templates()
    ' Display the names in C:\ that represent directories.
    Const MyPath = "c:\templates\"    ' Set the path.
    FindFiles MyPath, "*.dot"
End Sub

Sub FindFiles(strFolder As String, strFilePattern As String)
    Dim strFileName As String
    Dim strFolders() As String
    Dim iFolderCount As Integer
    Dim i As Integer
   
    'collect child folders
    strFileName = Dir$(strFolder & "\", vbDirectory)
    Do Until strFileName = ""
        If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
            If Left$(strFileName, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder & "\" & strFileName
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFileName = Dir$()
    Loop
   
    'process files in current folder
    strFileName = Dir$(strFolder & "\" & strFilePattern)
    Do Until strFileName = ""
            Documents.Open strFolder & "\" & strFileName
            'Do things to template here*****************
            DoEvents
            ActiveDocument.Close wdSaveChanges
        strFileName = Dir$()
    Loop
   
    'look through child folders
    For i = 0 To iFolderCount - 1
        FindFiles strFolders(i), strFilePattern
    Next i
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
robjeevesAuthor Commented:
Fantastic. that worked a treat.  Thanks very much

Rob
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.