A
Allen H
I have a simple routine that I modified from a posting by Andy Gallagher to
list all the files in a single folder in a word document.
I need a routine to list all the files in a folder and all its' subfolders.
Any suggestions would be appreciated.
Sub DirList()
'
Dim directoryname As String
'browse for folder
directoryname = GetFolderName("Choose a folder")
Set fso = CreateObject("Scripting.FileSystemObject")
Set mainfolder = fso.GetFolder(directoryname)
Set filecollection = mainfolder.Files
For Each file In filecollection
Selection.Text = file.Name
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Next
End Sub
_______________________________________
Function GetFolderName(sCaption As String) As String
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem
On Error GoTo CleanUp
Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item
GetFolderName = Item.Path
CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing
End Function
list all the files in a single folder in a word document.
I need a routine to list all the files in a folder and all its' subfolders.
Any suggestions would be appreciated.
Sub DirList()
'
Dim directoryname As String
'browse for folder
directoryname = GetFolderName("Choose a folder")
Set fso = CreateObject("Scripting.FileSystemObject")
Set mainfolder = fso.GetFolder(directoryname)
Set filecollection = mainfolder.Files
For Each file In filecollection
Selection.Text = file.Name
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeParagraph
Next
End Sub
_______________________________________
Function GetFolderName(sCaption As String) As String
'Needs a reference to (Tools > Reference)
'Microsoft Shell Controls And Automation
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Dim oItems As Shell32.FolderItems
Dim Item As Shell32.FolderItem
On Error GoTo CleanUp
Set oShell = New Shell
Set oFolder = oShell.BrowseForFolder(0, sCaption, 0)
Set oItems = oFolder.Items
Set Item = oItems.Item
GetFolderName = Item.Path
CleanUp:
Set oShell = Nothing
Set oFolder = Nothing
Set oItems = Nothing
Set Item = Nothing
End Function