Option Explicit
Private Errcnt As Integer
Sub Go_Find(filename As String, startfolder As String)
'Door Mark xl
'Zoek bestand "filename"
'in map "Startfolder"
Dim strpath As String
strpath = FindFilePath("D:\", filename, True) 'de functie FindfilePath geeft de locatie
'van het bestand terug
If strpath = "" Then
MsgBox "Het bestand " & filename & " is niet gevonden.. (" & Errcnt & " error(s))"
Else
Workbooks.Open strpath
End If
End Sub
Function FindFilePath(startfolder As String, filename As String, rootdir As Boolean) As String
'doorzoek alle mappen en submappen van map "Startfolder" totdat bestand "filename" gevonden is
Dim fs, fld, sfld, fldr, file
Dim strpath As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set fld = fs.getfolder(startfolder)
'zoek bestanden uit de root
If rootdir Then
'bestanden in de root directory
For Each file In fld.Files
If file.Name = filename Then
FindFilePath = file.Path
GoTo Foldererror:
End If
Next
End If
'submappen doorzoeken
For Each fldr In fld.SubFolders
Set sfld = fs.getfolder(fldr)
On Error GoTo Foldererror:
'eerst bestanden in submap doorzoeken
For Each file In sfld.Files
If file.Name = filename Then
FindFilePath = file.Path
GoTo Foldererror:
End If
Next
On Error GoTo 0
'daarna submappen doorzoeken
strpath = FindFilePath(sfld.Path, filename, False)
'gevonden bestand doorgeven aan alle niveaus
If strpath <> "" Then
FindFilePath = strpath
GoTo Foldererror:
End If
Nextfldr:
Next
Foldererror:
Select Case Err.Number
Case 0 'geen error, afsluiten
Case 70 'map is beveiligd
Resume Nextfldr:
Case Else:
Errcnt = Errcnt + 1
End Select
Set fs = Nothing
Set fld = Nothing
Set sfld = Nothing
End Function