Dear Experts,
I have the code below that would list all the files in selected directory.
It work so far the error happens when I click Cancel.
Can anyone is kind to help me fix the error?
Thank you in advance.
12Rev79
I have the code below that would list all the files in selected directory.
It work so far the error happens when I click Cancel.
Can anyone is kind to help me fix the error?
VBA Code:
Private Sub OldName_Click()
Dim fldr As FileDialog
Dim sItem As String
Dim strPath As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
Set fldr = Nothing
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim mysub As Object
Dim i As Integer
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sItem)
i = 1
For Each objFile In objFolder.Files
Cells(i + 2, 2) = objFile.Name
Cells(i + 2, 4) = objFile.Path
i = i + 1
Next objFile
For Each mysub In objFolder.SubFolders
Cells(i + 2, 2) = mysub.Name
Cells(i + 2, 4) = mysub.Path
i = i + 1
Next mysub
End Sub
Thank you in advance.
12Rev79
Last edited by a moderator: