Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

open files from listbox on userform within subfolder

0

Hi

based on first attached  will open  file from  listbox on userfor  within  folder

based on second  attached  I  want  also  open file  within subfolder,  I  try  add  function as  John suggestion , but  gives  object required in this line 

Set ObjFolder = "C:\Users\MU\Desktop\pdf Folder\" & .List(.ListIndex, 1) & ".pdf"

any  help  to  fix  this  problem ?

Answer
Discuss

Discussion

Mussala. Can't check now (I'm out, on mobile phone and can't open macro files) but a folder name should not end in ".pdf".

I've added that point to my Answer.
John_Ru (rep: 6142) Jul 11, '23 at 1:46 pm
Add to Discussion

Answers

0
Selected Answer

Mussala

As I said earlier, a folder name should not end in ".pdf".

Revised Answer 14 July 2023

From your clarification comments, you want to search for pdfs in a top-level folder (e.g. your C:\Users\MU\Desktop\pdf Folder\) and any subfolders, looking for a file matching the item clicked in the ListBox (e.g. NO_0003135 from the SALES worksheet).

In the attached revised file, I've changed the code so it searches the top-level then calls a function to search any subfolders. That function calls itself iteratively so the first line:

If Fnd <> vbNullString Then Exit Function

stops it going further after a match is found. In other words it stops when it finds the first match (so file names should be unique).

It relies on the variables declared at the top of the UserForm code page, so:

Public Sk As String, Fnd As String

where Sk is intended for what we seek (e.g. an Invoice Number) in the folder (and subfolders) and Fnd is the full path of what we find (if anything).

If the main code or function finds a filename (e.g. NO_000313.pdf) matching the picked item, it will offer you the choice of opening the file. If not it tells you it couldn't be found. 

Your click procedure below is modified (and other users must change the top-level path in bold): My comments should help you to follow it:

Private Sub ListBox1_Click()
    Dim Rply As Long

    Set objFso = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next

    With ListBox1
        'change path to suit
        Set objFolder = objFso.GetFolder("C:\Users\MU\Desktop\pdf Folder\")

        'clear found public varable
        Fnd = vbNullString

        If objFolder Is Nothing Then
            MsgBox "Please check macro- the named folder does not exist"
            Exit Sub
        End If
        ' set public file name to find
        Sk = LCase(.List(.ListIndex, 1)) & ".pdf"
        ' loop through top level folder
        For Each objSubFile In objFolder.Files
            If LCase(objSubFile.Name) = Sk Then
                ' if found, write file path to found variable
                Fnd = objSubFile.Path
                'stop looking
                Exit For
            End If
        Next objSubFile

        ' if not yet found, call the function to loop through its subfolders (and theirs)
        If Fnd = vbNullString Then LoopEachFolder objFolder, Sk

        ' quit FSO
        Set objFso = Nothing


        ' If a file was found...
        If Fnd <> vbNullString Then
            Rply = MsgBox(Fnd & " was found." & vbCr & vbCr & "Open that file?", vbYesNo, "File found...")
            ' ... open pdf if yes
            If Rply = vbYes Then apiShellExecute 0, "Open", Fnd, "", "", vbMaximizedFocus

            Else
            ' otherwise tell user
                MsgBox "Sorry but the file " & vbCr & .List(.ListIndex, 1) & ".pdf" _
                        & " was not found in " & objFolder & vbCr _
                        & " (or its subfolders)"
        End If

    End With

End Sub

Function LoopEachFolder(fldFolder As Object, Sk As String)

    ' With the subfolders in this folder....
    For Each objFldLoop In fldFolder.subFolders
        ' if already found, quit any iterative functions
        If Fnd <> vbNullString Then Exit Function
        ' ...otherwise loop through files in this folder
        For Each objSubFile In objFldLoop.Files
            If LCase(objSubFile.Name) = Sk Then
                ' found so add file path to found variable
                Fnd = objSubFile.Path
            End If
        Next objSubFile
        ' ... if not found, run the function on each subfolder found
        If Fnd = vbNullString Then LoopEachFolder objFldLoop, Sk

    Next objFldLoop

End Function

Hope this fixes things for you.

Discuss

Discussion

Hi John,
unfortunately  doesn't  work totally whether based on  orginal subject you  have  ever solved or  in  this modifying !
it  will shows message folder doesn't  existed 
I  suppose when click row  in listbox  and  the  file  is  not  existed  should  say  the  file  is  not  existed, not folder .  all of  the  files  where show  in  listbox  will be distributed in folders  and  subforlders. not  each  file contains  folder individual , each  group files will be in folder and  subfolder for  all of  files are  showed in listbox.
Mussala (rep: 12) Jul 12, '23 at 4:48 am
Mussala. The file does work (as I intended) but, as my question hinted, I don't know your directory structure. I suspect you used the function incorrectly- it aims to report all files in a folder/ subfolder but are you just trying to open a file somewhere in the structure?

Please edit your question to attach a new vesrion of the second file- one in which you have embedded a screenshot of the folders you want to report and where an example file is located..
John_Ru (rep: 6142) Jul 12, '23 at 6:28 am
John,
  let's  take  some  examples 
some  pdf files  are  existed in  this  directory C:\Users\MU\Desktop\pdf Folder\ 
some pdf files are  existed in  this  directory C:\Users\MU\Desktop\pdfFolder\ invoices\   and  some  pdf  files  are  existed in this  directory  C:\Users\MU\Desktop\pdf Folder\ invoices\JUL\
so  when  click pdf file  should  open regardless where  is exacly in  the  folder. the  most important   is matching  file name  in listbox  with file  name  in directory and  open  it  when  click.
Mussala (rep: 12) Jul 12, '23 at 1:44 pm
Mussala. I think I've done what you wanted- please see "Revised Answer 14 July 2023" and the replacement file.
John_Ru (rep: 6142) Jul 14, '23 at 5:53 am
This  is  really awesome, John !
thank  you  so  much .
Mussala (rep: 12) Jul 15, '23 at 9:46 am
Great, glad it worked as you wanted. Thanks for selecting my Answer, Mussala.
John_Ru (rep: 6142) Jul 15, '23 at 10:56 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login