Application.FileSearch code replacement

pmerkel37

New Member
Joined
Nov 6, 2002
Messages
5
Hi - we have had an excel file running for years and now we have upgraded to Excel 2007.

The file now stops with an error around a Application.FileSearch command in the macro.

Thanks in advance

Here is the macro:

Sub Linkages00() 'opens each template, updates the links, and saves it
Set fs = Application.FileSearch
Application.StatusBar = "Linkages: Refreshing Linkages..."
With fs
.Filename = "*.xls"
.LookIn = DrvName & "\testmdus\download\" & Right(RunYr, 2) & RunMo & "\"
.Execute
For i = 1 To .foundfiles.Count
On Error Resume Next
Workbooks.Open Filename:=.foundfiles(i), UpdateLinks:=1, ReadOnly:=False, _
WriteResPassword:="nav", IgnoreReadOnlyRecommended:=True
On Error GoTo 0
ActiveWorkbook.Save
ActiveWorkbook.Close
Next i
End With
Application.StatusBar = "Linkages has completed."
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Search the board on

Excel 2007 FileSearch


You should find sevaral threads on the subject. That is pretty much the biggest complaint people have about 2007
 
Upvote 0
Haven't tested the code, but if you look in the XL2007 VBA help files, the Application.FileSearch still seems to be applicable in the same format so I don't think it's a compatibility issue.

The only thought I have is, have you defined the variable fs?

Look at the very top of your module, do you have the line:
Option Explicit

If you do, then you must define all variables before using them. Either delete this line (not recommended!) or, just before the line:
Set fs = Application.FileSearch

Put this:
Dim fs as Variable
 
Upvote 0
Ok, too slow!! Looks like there is more of a problem than I realised!!!
 
Upvote 0
pmerkel37

There at least 2 options.

1 Use Dir.

2 Use the FileSystemObject.

Lewiy

FileSearch definitely not available in Excel 2007 VBA.

Well it wasn't when I trialled it, and I've got to second JonMo, it's one of the most common thing complained about.

Along with the 'ribbon', trying to find commonly used functions, the general look etc. :)
 
Upvote 0
moz-screenshot.jpg
Lewiy

FileSearch definitely not available in Excel 2007 VBA.

Admittedly I've not tried to use it, but it's certainly in the help files. Why do they torment us with features we can't use???!!!!
 
Upvote 0
1 - Create a class with FileSearchObject name and the following code:
Code:
'This source code is provided "as is" and without warranties as to performance or merchantability.
'The author and/or distributors of this source code may have made statements about this source code.
'Any such statements do not constitute warranties and shall not be relied on by the user in deciding
'whether to use this source code.
'
'This source code is provided without any express or implied warranties whatsoever. Because of the
'diversity of conditions and hardware under which this source code may be used, no warranty of fitness
'for a particular purpose is offered. The user is advised to test the source code thoroughly before
'relying on it. The user must assume the entire risk of using the source code.


'Class developed by Henrique Heidemann Cardoso
'If you have any commentaries please address it to hheidemann@gmail.com
'+551297067969

'You can distribute this code freely provided you do not remove the notes above

Private fileNameMask As String
Private fileNameMaskText As String
Private fileNameMaskExtText As String
Private fileNameMaskVector() As String
Private fileNameMaskExtVector() As String
Private fs As Scripting.FileSystemObject
Private searchMethod As String

Private Sub setMask(ByVal fileNameMaskToMatch As String)
    
    fileNameMask = fileNameMaskToMatch
    
    Dim dotPosition As Long
    dotPosition = InStrRev(fileNameMask, ".", -1, vbTextCompare)
    
    If dotPosition > 0 Then
        searchMethod = "NameAndExtension"
        fileNameMaskText = Left(fileNameMask, dotPosition - 1)
        fileNameMaskExtText = Right(fileNameMask, Len(fileNameMask) - dotPosition)
        
        If InStr(1, fileNameMaskText, "*", vbTextCompare) > 0 Then
            searchMethod = searchMethod & "WithWildChars"
            fileNameMaskVector = Split(fileNameMaskText, "*", -1, vbTextCompare)
        End If
        
        
        If InStr(1, fileNameMaskExtText, "*", vbTextCompare) > 0 Then
            searchMethod = searchMethod & "WithWildCharsInExtension"
            fileNameMaskExtVector = Split(fileNameMaskExtText, "*", -1, vbTextCompare)
        End If
        
    Else
        searchMethod = "NameOnly"
        
        If InStr(1, "*", fileNameMask, vbTextCompare) > 0 Then _
            searchMethod = searchMethod & "WithWildChars"
    
    End If
End Sub
Private Function doesNameMatch(ByVal FileName As String) As Boolean
    
    Dim dotPosition As Long
    dotPosition = InStrRev(FileName, ".", -1, vbTextCompare)
    
    Dim fileNameText As String
    Dim fileNameExtText As String
    fileNameText = Left(FileName, dotPosition - 1)
    fileNameExtText = Right(FileName, Len(FileName) - dotPosition)

    If searchMethod = "NameAndExtension" Then
        
        If StrComp(fileNameMask, FileName, vbTextCompare) = 0 Then
            doesNameMatch = True
        Else
            doesNameMatch = False
        End If
        
    ElseIf searchMethod = "NameAndExtensionWithWildChars" Then
        
        If StrComp(fileNameMaskExtText, fileNameExtText, vbTextCompare) = 0 Then
            
            If specialStrComp(fileNameMaskVector, fileNameText) = 0 Then
                doesNameMatch = True
            Else
                doesNameMatch = False
            End If
        Else
            doesNameMatch = False
        End If
        
    ElseIf searchMethod = "NameAndExtensionWithWildCharsWithWildCharsInExtension" Then
        
        If specialStrComp(fileNameMaskExtVector, fileNameExtText) = 0 Then
            
            If specialStrComp(fileNameMaskVector, fileNameText) = 0 Then
                doesNameMatch = True
            Else
                doesNameMatch = False
            End If
        Else
            doesNameMatch = False
        End If
        
    ElseIf searchMethod = "NameOnly" Then
    
        If StrComp(fileNameMask, FileName, vbTextCompare) = 0 Then
            doesNameMatch = True
        Else
            doesNameMatch = False
        End If
    
    ElseIf searchMethod = "NameOnlyWithWildChars" Then
    
        If specialStrComp(fileNameMaskVector, FileName) = 0 Then
            doesNameMatch = True
        Else
            doesNameMatch = False
        End If
            
            
    End If
    
End Function
Private Function specialStrComp(ByRef aStringVector() As String, _
                                ByVal aString As String) As Integer

    Dim i As Long
    Dim cursor As Long
    
    Dim ittString As String
    cursor = 1
    specialStrComp = 0
    
    For i = 0 To UBound(aStringVector)
    
       If cursor > Len(aString) Then
            specialStrComp = -1
            Exit For
        End If
        
        ittString = aStringVector(i)
        If Len(Trim$(ittString)) <= 0 Then
            cursor = cursor + 1
        Else
            If i > 0 Then
                If Len(Trim$(aStringVector(i - 1))) > 0 Then _
                    cursor = cursor + 1
            End If
            
            If i = 0 Then
                If StrComp(ittString, Left(aString, Len(ittString)), vbTextCompare) <> 0 Then
                    specialStrComp = -1
                    Exit For
                Else
                    cursor = cursor + Len(ittString)
                End If
            ElseIf i = UBound(aStringVector) Then
                If StrComp(ittString, Right(aString, Len(ittString)), vbTextCompare) <> 0 Then
                    specialStrComp = -1
                Else
                    specialStrComp = 0
                End If
                Exit For
            Else
                Dim ittFoundPos As Integer
                ittFoundPos = InStr(cursor, aString, ittString, vbTextCompare)
                If Not (ittFoundPos > 0) Then
                    specialStrComp = -1
                    Exit For
                Else
                    cursor = ittFoundPos + Len(ittString)
                End If
            End If
            
        End If
        
    Next i

End Function
Sub alternateFileSearch(ByVal initialPath As String, _
                        ByVal fileNameMaskToSearch As String, _
                        ByRef foundFiles As Collection)
    
    
    setMask fileNameMaskToSearch
    Dim oFolder As Scripting.Folder
    If fs.FolderExists(initialPath) Then
        
        If foundFiles Is Nothing Then _
            Set foundFiles = New Collection
        
        Set oFolder = fs.getFolder(initialPath)
        recursiveSearchForFiles oFolder, foundFiles
    End If
    
End Sub
Private Sub recursiveSearchForFiles(ByRef oFolder As Scripting.Folder, _
                                    ByRef foundFiles As Collection)
    
    addFilesToColection oFolder, foundFiles
    
    Dim ittFolder As Scripting.Folder
    For Each ittFolder In oFolder.SubFolders
        recursiveSearchForFiles ittFolder, foundFiles
    Next ittFolder
        
                        
End Sub


Private Sub addFilesToColection(ByRef oFolder As Scripting.Folder, _
                                ByRef foundFiles As Collection)
    
    Dim ittFile As Scripting.File
    For Each ittFile In oFolder.Files
    
        If doesNameMatch(ittFile.Name) Then _
            foundFiles.Add ittFile.Path
        
    Next ittFile
End Sub

Private Sub Class_Initialize()
    Set fs = New Scripting.FileSystemObject
End Sub

Private Sub Class_Terminate()
    Set fs = Nothing
End Sub

2 - This would be your code to look into "C:\" and its subdirectories for all "*.txt" files:

Code:
Dim myFileSearchObject As FileSearchObject
Dim foundFilesColl As Collection
Set myFileSearchObject = New FileSearchObject
myFileSearchObject.alternateFileSearch "C:\", "*.txt", foundFilesColl
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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