Private Declare Function FindExecutable Lib "shell32" _
Alias "FindExecutableA" _
(ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal sResult As String) As Long
Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" _
(ByVal nSize As Long, _
ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
Private Const ERROR_FILE_NO_ASSOCIATION As Long = 31
Private Const ERROR_FILE_NOT_FOUND As Long = 2
Private Const ERROR_PATH_NOT_FOUND As Long = 3
Private Const ERROR_FILE_SUCCESS As Long = 32 'my constant
Private Const ERROR_BAD_FORMAT As Long = 11
Private Sub Command1_Click()
Dim success As Long
Dim sBrowser As String
'success is passed and filled in the routine
sBrowser = GetBrowserName(success)
'possible return values from the call
'returned in success
Select Case success
'the call succeeded
Case Is >= ERROR_FILE_SUCCESS
MsgBox sBrowser
Exit Sub
'other possible return values
Case ERROR_FILE_NO_ASSOCIATION
Case ERROR_FILE_NOT_FOUND
Case ERROR_PATH_NOT_FOUND
Case ERROR_BAD_FORMAT
Case Else:
End Select
'if this far the call failed
MsgBox "No dice!"
End Sub
Private Function GetBrowserName(dwFlagReturned As Long) As String
Dim hFile As Long
Dim sResult As String
Dim sTempFolder As String
'get the user's temp folder
sTempFolder = GetTempDir()
'create a dummy html file in the temp dir
hFile = FreeFile
Open sTempFolder & "dummy.html" For Output As #hFile
Close #hFile
'get the file path & name associated with the file
sResult = Space$(MAX_PATH)
dwFlagReturned = FindExecutable("dummy.html", sTempFolder, sResult)
'clean up
Kill sTempFolder & "dummy.html"
'return result
GetBrowserName = TrimNull(sResult)
End Function
Private Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
Public Function GetTempDir() As String
Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
Call GetTempPath(nSize, tmp)
GetTempDir = TrimNull(tmp)
End Function