The code below lists files in a folder.
The first Sub uses a Browse Window dialogue to get the folder path. This one works.
The second Sub just has the the folder path in the code. But it doesn't work. Is there a reason for this?
Sub Test_1()
Dim BrowseWindow As FileDialog
Dim FSO As Object, SourceFolder As Object, SubFolder As Object
Dim FileItem As Object
Dim ThePath As String
Set BrowseWindow = Application.FileDialog(msoFileDialogFolderPicker)
If BrowseWindow.Show = -1 Then
ThePath = BrowseWindow.SelectedItems(1)
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(ThePath)
For Each FileItem In SourceFolder.Files
Debug.Print "File found"
Next FileItem
Set FileItem = Nothing: Set SourceFolder = Nothing: Set FSO = Nothing
End Sub
Sub Test_2()
Dim FSO As Object, SourceFolder As Object, SubFolder As Object
Dim FileItem As Object
Dim ThePath As String
ThePath = "C:\Test"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = FSO.GetFolder(ThePath)
For Each FileItem In SourceFolder.Files
Debug.Print "File found"
Next FileItem
Set FileItem = Nothing: Set SourceFolder = Nothing: Set FSO = Nothing
End Sub
Bookmarks