2007 Excel , No Application.FileSearch anymore Help

myworld

New Member
Joined
Dec 8, 2009
Messages
5
Hi,

Just got the new excel 2007 and foud it doesn't have the Application.FileSearch anymore, which we use quite a bit. I've been trying to work a way around it but have had no luck. I'm on a time crunch so thought for the first time I'll ask the internet for the first time out of desperation. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
What I need to do is use VBA to look into a folder and return the file names that are in the folder, in a column on an excel sheet and each of the names be a hyperlink to the file. The code I used before is below. I would greatly appreciate any help!!!<o:p></o:p>


Sub Allfiles()<o:p></o:p>
Dim myPath As String<o:p></o:p>
Dim lLen As Long, i As Long<o:p></o:p>
Dim parts<o:p></o:p>
<o:p> </o:p>
myPath = ThisWorkbook.Path & "\Files"
With Application.FileSearch<o:p></o:p>
.NewSearch<o:p></o:p>
.LookIn = myPath<o:p></o:p>
.SearchSubFolders = True<o:p></o:p>
.Filename = "*.*"<o:p></o:p>
Range("j2:k6000").Select<o:p></o:p>
Selection.ClearContents<o:p></o:p>
If .Execute() > 0 Then<o:p></o:p>
For i = 2 To .FoundFiles.Count<o:p></o:p>
parts = <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Split</st1:place></st1:City>(Trim(Replace(Dir(.FoundFiles(i)), ".xls", "")))<o:p></o:p>
Cells(i, 8) = parts(UBound(parts))<o:p></o:p>
Cells(i, 9).FormulaR1C1 = "=Hyperlink(" & Chr(34) & .FoundFiles(i) _<o:p></o:p>
& Chr(34) & ",R[0]C[-1])"
<o:p> </o:p>
Else<o:p></o:p>
Range("a1").Select<o:p></o:p>
MsgBox "There were no files found."<o:p></o:p>
End If<o:p></o:p>
End With<o:p></o:p>
End Sub
 
After adding "Microsoft scripting Runtime" reference you can use below recursive function code for find files:

Code:
Dim xFile As File
Dim ObjFolder As Folder
Dim i As Long
Dim ws As Worksheet

Sub Allfiles()

    
    Set Fsys = CreateObject("Scripting.FileSystemObject")
    Set ws = ActiveSheet
    
    ' Static path
    Set ObjFolder = Fsys.GetFolder(ThisWorkbook.Path & "\Files" )
    
    i = 2
    
    Call FindFolder(ObjFolder)
        
    
    
End Sub
    
Function FindFolder(FolderPath As Folder) As Boolean
    
    Dim RootFolder As Folder
    ' Check for subfolders
    For Each RootFolder In FolderPath.SubFolders
        
        Call FindFiles(RootFolder)
        
        Call FindFolder(RootFolder)
            
    Next
    
End Function

Function FindFiles(xFolder As Folder)
    
    For Each xFile In xFolder.Files
            
        ws.Hyperlinks.Add Anchor:=ws.Range("I" & i), Address:=xFile.Path, TextToDisplay:=xFile.Name
        i = i + 1
        
    Next
    
End Function
 
Last edited:
Upvote 0
Thank You Very Much Somnath, I turned on the "Microsoft scripting Runtime" tools>reference then I put the code in and triggered it, first it error'ed cause I changed the folder name so I fixed that. Then I triggered it again (i added it to a button) and it just blinked? Not sure why it did not doing anything else? Though I do thank you greatly for going to the bother of writting this up in the first place :-)
 
Upvote 0
Oh yes... I think you don't have any sub folder in "Files" folder and all files exist in "Files" folder only...

Just try below updated code:



Code:
Dim xFile As File
Dim ObjFolder As Folder
Dim i As Long
Dim ws As Worksheet

Sub Allfiles()

    
    Set Fsys = CreateObject("Scripting.FileSystemObject")
    Set ws = ActiveSheet
    
    ' Static path
    Set ObjFolder = Fsys.GetFolder(ThisWorkbook.Path & "\Files" )
    
    i = 2
    
    Call FindFiles(ObjFolder)
    Call FindFolder(ObjFolder)
    
    
    
End Sub
    
Function FindFolder(FolderPath As Folder) As Boolean
    
    Dim RootFolder As Folder
    ' Check for subfolders
    For Each RootFolder In FolderPath.SubFolders
        
        Call FindFiles(RootFolder)
        
        Call FindFolder(RootFolder)
            
    Next
    
End Function

Function FindFiles(xFolder As Folder)
    
    For Each xFile In xFolder.Files
            
        ws.Hyperlinks.Add Anchor:=ws.Range("I" & i), Address:=xFile.Path, TextToDisplay:=xFile.Name
        i = i + 1
        
    Next
    
End Function

Let me know how it works...
 
Upvote 0
You are a master!!!! Thanks you very much for doing this! This means heaps! Again thank you very very much :) Worked like a dream!!! :) Thanks
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top