For example, if I have the following files in a folder
absence_from_home_wb3.doc
age_changes_wb3.doc
agents_wb3.doc
annual_review_wb3.doc
FileSearch sorts them in that order (which is also the way that Windows Explorer sorts them, alphabetically ...).
Using my FileSearch replacement, however, the order of the second and third files is reversed - "agents_wb3.doc" now comes before "age_changes_wb3.doc" (it seems that with this option, letters precede underscores).
I use Windows XP.
My FileSearch code is:
Code: Select all
Public Sub FileSearchOption()
Dim lngEachFileFound As Long
With Application.FileSearch
.NewSearch
.FileName = "*.doc"
.LookIn = "c:\wb3\"
.SearchSubFolders = True
.FileType = msoFileTypeWordDocuments
.Execute (msoSortByNone)
For lngEachFileFound = 1 To .FoundFiles.Count
Documents.Open FileName:=.FoundFiles(lngEachFileFound)
With ActiveDocument
MsgBox .Name
.Close SaveChanges:=wdDoNotSaveChanges
End With
Next lngEachFileFound
End With
End Sub
My replacement code is:
Code: Select all
Public Sub DirOption()
DirectoryFileSearch "c:\wb3\"
End Sub
Private Sub DirectoryFileSearch(Path As String)
Dim strDirectory As String
Dim strDirectoryList() As String
Dim intArraySize As Integer
Dim intPosition As Integer
strDirectory = Dir(Path, vbDirectory)
Do While strDirectory <> ""
If strDirectory <> "." And strDirectory <> ".." Then
If (GetAttr(Path & strDirectory) And vbDirectory) = vbDirectory Then
If (Not strDirectoryList) = True Then
ReDim strDirectoryList(0 To 0)
Else
ReDim Preserve strDirectoryList(0 To UBound(strDirectoryList) + 1)
End If
strDirectoryList(UBound(strDirectoryList)) = strDirectory
Else
intPosition = InStrRev(strDirectory, ".")
If intPosition > 0 Then
If InStr(1, "doc", LCase(Right(strDirectory, Len(strDirectory) - intPosition))) Then
Documents.Open FileName:=Path & strDirectory
With ActiveDocument
MsgBox .Name
.Close SaveChanges:=wdDoNotSaveChanges
End With
End If
End If
End If
End If
strDirectory = Dir
Loop
If (Not strDirectoryList) = True Then
Else
For intArraySize = 0 To UBound(strDirectoryList)
DirectoryFileSearch Path & strDirectoryList(intArraySize) & Application.PathSeparator
Next
End If
End Sub