Hello,
the code opens files are existed in folder but ignore subfolders and sub subfolders .
so I search for way to expand code to including all file in main folder and folders branches in directory
here is the code
Private Sub ListBox1_Click()
Dim FileRoot As String
Dim FileName As String
Const FolderPath = "C:\Users\spp\Desktop\information\"
With ListBox1
MsgBox .ListIndex & ": " & .List(.ListIndex, 2)
FileRoot = .List(.ListIndex, 2)
End With
FileName = Dir(objFldr& FileRoot & ".xls*")
If FileName <> "" Then
Workbooks.Open FileName:=objFldr& FileName
Else
FileName = Dir(objFldr& FileRoot & ".pdf")
If FileName <> "" Then
ActiveWorkbook.FollowHyperlink objFldr& FileName
Else
MsgBox "File not found for " & FileRoot
End If
End If
End Sub
here is my trying
Private Sub ListBox1_Click()
Dim FileRoot As String
Dim FileName As String
Dim objFldr As Object
Dim objFSO As Object
Dim objSubFldr As Object
Const FolderPath = "C:\Users\pc\Desktop\data\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFldr = objFSO.GetFolder(FolderPath)
LoopEachFolder objFldr
With ListBox1
MsgBox .ListIndex & ": " & .List(.ListIndex, 2)
FileRoot = .List(.ListIndex, 2)
End With
FileName = Dir(FolderPath & FileRoot & ".xls*")
If FileName <> "" Then
Workbooks.Open FileName:=FolderPath & FileName
Else
FileName = Dir(FolderPath & FileRoot & ".pdf")
If FileName <> "" Then
ActiveWorkbook.FollowHyperlink FolderPath & FileName
Else
MsgBox "File not found for " & FileRoot
End If
End If
End Sub
Function LoopEachFolder(fldFolder As Object)
Dim Fname As String
Dim objFldLoop As Object
Fname = Dir(fldFolder & "\")
' List files in this folder
Do While Fname <> ""
Fname = Dir()
Loop
' With the subfolders in this folder....
For Each objFldLoop In fldFolder.subFolders
' run this function on each subfolder found
LoopEachFolder objFldLoop
Next objFldLoop
End Function
thanks