98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel Training and help » Application.Filesearch
Application.Filesearch
Resolved · Urgent Priority · Version 2007
Jonathan has attended:
No courses
Application.Filesearch
Hi there,
I had written a macro in Excel 2003 which searches a directory and lists all of the files in that directory, of type *.ssn and writes this list to a worksheet. To do this i used the Application.Filesearch functionality. Having upgraded to Excel 2007 this no longer works and it would appear the Filesearch functionality has been removed. The code i used is below - could you suggest how i can get this working in Excel 2007 please. Note I no longer need it to be able to work in Excel 2003.
Regards,
Jonathan
Sub ListAllFiles()
Dim fs As FileSearch
Dim ws As Worksheet
Dim i As Long
Set fs = Application.FileSearch
'Clear the current File List
Sheets("MISC").Range("AL49:AL128").ClearContents
'Create a new File List - all *.ssn files in the strDataLocation data directory
With fs
.SearchSubFolders = False ' set to true if you want sub-folders included
'.FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
.Filename = "*.ssn" 'Can use wildcards for file types.
.LookIn = strDataLocation 'modify this to where you want to serach
If .Execute > 0 Then
Set ws = Worksheets("MISC")
For i = 1 To .FoundFiles.Count
ws.Cells(i + 48, 38) = Mid$(.FoundFiles(i), InStrRev(.FoundFiles(i), "\") + 1)
Next
Else
MsgBox "No files found"
End If
End With
End Sub
RE: Application.Filesearch
Hi Jonathon
Thanks for your question
The removal of the filesearch method in 2007 is annoying, and puzzling.
It is possible to use the Dir method, which is very old dating back to pre windows versions of basic. It is hard to give a precise example of how to use this as I would need to know more about your file structures etc. But I suggest some sort of loop , using the Dir method, that looks at each file in turn and then writes it to the sheet if its extension matches your category.
I have found the following link that outlines some uses of the dir method, which you might find useful.
http://www.ozgrid.com/forum/showpost.php?p=369573& ;postcount=4
If you have any further queries please do not hesitate to get back to me.
Regards
Stephen
Tue 2 Feb 2010: Automatically marked as resolved.
Training information:
See also:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Manually rotating a 3D chartTo manually rotate a 3D chart; |