Private Declare Function SearchPath _
Lib "kernel32" Alias "SearchPathA" ( _
ByVal lpPath As String, _
ByVal lpFileName As String, _
ByVal lpExtension As String, _
ByVal nBufferLength As Long, _
ByVal lpBuffer As String, _
ByVal lpFilePart As String) As Long
Private Function SearchFile(ByVal sFileName As String)
'This function will search after the file in the following order
' 1. The directory the app is loaded from
' 2. The active directory
' 3. Windows System directory (winnt\system32 or windows\system)
' 4. The Windows directory
' 5. The directories listed in the PATH environment variable
Dim sBuffer As String
Dim nLen As Long
sBuffer = Space$(260)
nLen = SearchPath(vbNullString, sFileName, vbNullString, _
Len(sBuffer), sBuffer, vbNullString)
SearchFile = Left$(sBuffer, nLen)
End Function
Private Sub Command1_Click()
'This is an example on how to call the code.
'If the file isn't found an empty string is returned
MsgBox SearchFile("notepad.exe")
End Sub