Question : Application.FileSearch in Office 2007

Hi,

I have just upgraded from Office 2000 to Office 2007, the following macro used to work but now I get and error message 'Run time error 445 Object does not support this action'

   With Application.FileSearch
        .NewSearch
        .LookIn = "c:\files"
        .Filename = "*.xls"
        .FileType = msoFileTypeAllFiles
        If .Execute > 0 Then
        For i = 1 To .FoundFiles.Count
            myFileToOpen = .FoundFiles(i)
            Workbooks.Open Filename:=myFileToOpen
            myWindowTitle = ActiveWindow.Caption
' I then do stuff to it
            ActiveWindow.Close
        Next i
        End If
    End With

I have even tried copying the following example out of help and it fails as well
With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"
    .TextOrProperty = "Run"
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles
End With

Perhaps Application.FileSearch is broken??

What I am wanting to do is open/process all the xls files from a folder, I do not know the names of the files ahead of time.

Thanks,
Gary,

Answer : Application.FileSearch in Office 2007

Yup, for some reason Application.FileSearch is gone in 2007. I would use the FileSystemObject.

Create a reference to Microsoft Scripting Runtime then do this:

Sub openFiles()

    Dim FSO As New FileSystemObject
    Dim fil As File
    Dim dir As Folder
    Dim wbk As Workbook
   
    Set dir = FSO.GetFolder("C:\")
   
    For Each fil In dir.Files
        If Right(fil.Name, 4) = ".xls" Then
            Set wbk = Workbooks.Open(fil.Path)
            'Do stuff here
            wbk.Close False
        End If
    Next fil
   
End Sub

It's not recursive so It only scans the folder you specify. You can make it recursive without too much trouble.
Random Solutions  
 
programming4us programming4us