Hi Neil
This is not the best way, I'd probably use the FileSystemObject in the
Scripting library. I'll try to dig through some of my code to see if I have
an example but here's how to do it using the vanilla Dir approach:
Public Sub CreateFileList()
Dim docOutput As Word.Document
Dim strPath As String
Dim strMask As String
' Create document to hold file listing
Set docOutput = Documents.Add
' Path to start search at
strPath = "F:\My Templates\"
' Files to search for
strMask = "*.doc"
' Create listing
ListDocsInFolder docOutput, strPath, strMask
End Sub
Public Sub ListDocsInFolder(ByVal docList As Word.Document, _
ByVal strFolder As String, _
ByVal strFileMask As String)
Dim strFile As String
Dim varFolder As Variant
' Ensure folder path is terminated correctly
strFolder = Trim$(strFolder)
If Right$(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
' List all files in the current folder
strFile = Dir$(strFolder & strFileMask)
Do Until strFile = vbNullString
AddFileNameToDocument docList, strFolder, strFile
strFile = Dir$
Loop
' Now list files in any subfolders of the current folder
For Each varFolder In GetAllSubFolders(strFolder)
ListDocsInFolder docList, strFolder & varFolder, strFileMask
Next
End Sub
Private Function GetAllSubFolders(ByVal strPath As String) As Collection
Dim colFolders As New Collection
Dim strFolder As String
If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
strFolder = Dir$(strPath, vbDirectory)
Do Until LenB(strFolder) = 0
If strFolder <> "." And strFolder <> ".." Then
If (GetAttr(strPath & strFolder) And vbDirectory) = _
vbDirectory Then
colFolders.Add strFolder
End If
End If
strFolder = Dir$
Loop
' Return collection of this folders sub-folders
Set GetAllSubFolders = colFolders
End Function
Private Sub AddFileNameToDocument(ByVal docList As Word.Document, _
ByVal strFolder As String, _
ByVal strFile As String)
Dim rngEnd As Word.Range
Set rngEnd = docList.Content
rngEnd.Collapse wdCollapseEnd
' Add the path nad file name to the output document
rngEnd.InsertAfter strFolder & strFile & vbCr
End Sub
It creates a list of files in the current document using the drive/folder
specified:
strPath = "F:\My Templates\"
the file search mask is:
strMask = "*.doc"
HTH + Cheers - Peter