[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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
0
robjeeves
Asked:
robjeeves
1 Solution
 
GrahamSkanCommented:

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
 
robjeevesAuthor Commented:
Fantastic. that worked a treat.  Thanks very much

Rob
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now