ExcelWorker
Programmer
Someone out there might know a faster way of doing this but this subroutine works. It will let you find a file by giving it only the filename.
It will return the FULL path name.
-----------------------------------------------------
Option Explicit
Dim myFS As Object
Sub test()
Dim myDirPath As String
MY_FindFile "FY09 Workload Program.xls", myDirPath
Debug.Assert False
End Sub
Sub MY_FindFile(myFilename As String, myReturnedPath As String)
Dim myPath As String
Dim myTemp As String
Dim myFolder As Object
Dim mySub As Object
Dim myItem As Object
Dim myDriveCount As Integer
Set myFS = CreateObject("Scripting.FileSystemObject")
myDriveCount = 66
myPath = "A:\"
ReTestPath:
If myFS.driveexists(myPath) = False Then GoTo NextDrive
Set myFolder = myFS.GetFolder(myPath)
Set mySub = myFolder.subfolders
For Each myItem In mySub
myTemp = TestSub(myFilename, myItem.Path & "\")
If myTemp <> "" Then
myReturnedPath = VBA.Left(myTemp, VBA.InStrRev(myTemp, "\"))
Exit Sub
End If
NextItem:
Next
NextDrive:
myPath = VBA.Chr(myDriveCount) & ":\"
myDriveCount = myDriveCount + 1
GoTo ReTestPath
End Sub
Function TestSub(myFilename As String, myNewPath As String) As String
Dim myFolder As Object, mySub As Object, myItem As Object
Dim myTemp As String
If myFS.fileexists(myNewPath & myFilename) = True Then
TestSub = myNewPath & myFilename
Exit Function
End If
Set myFolder = myFS.GetFolder(myNewPath)
Set mySub = myFolder.subfolders
On Error GoTo TestError
For Each myItem In mySub
myTemp = TestSub(myFilename, myItem.Path & "\")
If myTemp <> "" Then
TestSub = myTemp
Exit Function
End If
Next
Exit Function
TestError:
If Err.Description = "Permission denied" Then Exit Function
Debug.Assert False
End Function
It will return the FULL path name.
-----------------------------------------------------
Option Explicit
Dim myFS As Object
Sub test()
Dim myDirPath As String
MY_FindFile "FY09 Workload Program.xls", myDirPath
Debug.Assert False
End Sub
Sub MY_FindFile(myFilename As String, myReturnedPath As String)
Dim myPath As String
Dim myTemp As String
Dim myFolder As Object
Dim mySub As Object
Dim myItem As Object
Dim myDriveCount As Integer
Set myFS = CreateObject("Scripting.FileSystemObject")
myDriveCount = 66
myPath = "A:\"
ReTestPath:
If myFS.driveexists(myPath) = False Then GoTo NextDrive
Set myFolder = myFS.GetFolder(myPath)
Set mySub = myFolder.subfolders
For Each myItem In mySub
myTemp = TestSub(myFilename, myItem.Path & "\")
If myTemp <> "" Then
myReturnedPath = VBA.Left(myTemp, VBA.InStrRev(myTemp, "\"))
Exit Sub
End If
NextItem:
Next
NextDrive:
myPath = VBA.Chr(myDriveCount) & ":\"
myDriveCount = myDriveCount + 1
GoTo ReTestPath
End Sub
Function TestSub(myFilename As String, myNewPath As String) As String
Dim myFolder As Object, mySub As Object, myItem As Object
Dim myTemp As String
If myFS.fileexists(myNewPath & myFilename) = True Then
TestSub = myNewPath & myFilename
Exit Function
End If
Set myFolder = myFS.GetFolder(myNewPath)
Set mySub = myFolder.subfolders
On Error GoTo TestError
For Each myItem In mySub
myTemp = TestSub(myFilename, myItem.Path & "\")
If myTemp <> "" Then
TestSub = myTemp
Exit Function
End If
Next
Exit Function
TestError:
If Err.Description = "Permission denied" Then Exit Function
Debug.Assert False
End Function