Hi
this is the original code
the code will open pdf file by click selected row from listbox , but my problem the code doesn't deal with files are existed in subfolders within the main folder in the main directory
I try with this
but gives error object required in this line
how can I make this code deals with folders & subfolders when I try open PDF file ?
thanks
this is the original code
VBA Code:
Private Declare Function [B]apiShellExecute[/B] Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
VBA Code:
Private Sub ListBox1_Click()
[B] Dim Filename As String[/B]
With ListBox1
'change path to suit
Filename = "C:\Users\MU\Desktop\pdf Folder\" &[B] .List(.ListIndex, 1)[/B] & ".pdf"
' Test if file exists...
If Dir(Filename) <> vbNullString Then
'... if so, write column 2 value to text box
TextBox1 = .List(.ListIndex, 1)
' and open pdf
[B]apiShellExecute[/B] 0, "Open", Filename, "", "", vbMaximizedFocus
Else
' otherwise tell user and stop
MsgBox "Sorry but the following file was not found:" & vbCr & Filename
Exit Sub
End If
End With
'ListBox1.ListIndex = -1
End Sub
I try with this
VBA Code:
Private Sub ListBox1_Click()
Dim Filename As String
Set objFso = CreateObject("Scripting.FileSystemObject")
With ListBox1
'change path to suit
Set ObjFolder = "C:\Users\MU\Desktop\pdf Folder\" & .List(.ListIndex, 1) & ".pdf"
For Each objSubFile In ObjFolder.Files
If InStr(objSubFile.Name, ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' call the function to loop through its subfolders (and theirs)
LoopEachFolder ObjFolder
' quit FSO
Set objFso = Nothing
' Test if file exists...
If Dir(ObjFolder) <> vbNullString Then
'... if so, write column 2 value to text box
TextBox1 = .List(.ListIndex, 1)
' and open pdf
apiShellExecute 0, "Open", ObjFolder, "", "", vbMaximizedFocus
Else
' otherwise tell user and stop
MsgBox "Sorry but the following file was not found:" & vbCr & Filename
Exit Sub
End If
End With
'ListBox1.ListIndex = -1
End Sub
Function LoopEachFolder(fldFolder As Object)
' With the subfolders in this folder....
For Each objFldLoop In fldFolder.subFolders
' ...loop through files in this folder
For Each objSubFile In objFldLoop.Files
If InStr(objSubFile.Name, ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' ... and run the function on each subfolder found
LoopEachFolder objFldLoop
Next objFldLoop
End Function
VBA Code:
Set ObjFolder = "C:\Users\MU\Desktop\pdf Folder\" & .List(.ListIndex, 1) & ".pdf"
thanks