CodeItBetter Programming Another VB Programming Blog

How to open a new instance of default browser and browse to a specific file

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'Internet - How to open a new instance of default browser and browse to a specific file
Public Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
    (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
 
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 
Private Const HKEY_CLASSES_ROOT = &H80000000
 
Public Sub ShellNewBrowser(ByVal URL As String, _
    Optional ByVal ShowWindowState As VbAppWinStyle = vbNormalFocus)
    Dim lRegKey As Long
    Dim sBrowser As String
    Dim sData As String * 260
    Dim lData As Long
 
    ' Open the registry key in the CLASSES_ROOT hive of the registry
    ' Which holds the path/file name of the default browser
    If RegOpenKey(HKEY_CLASSES_ROOT, "htmlfile\shell\open\command", lRegKey) = 0 Then
        lData = 260
        Call RegQueryValueEx(lRegKey, "", 0&, 0&, ByVal sData, lData)
        sBrowser = Left(sData, lData - 1)
        Call RegCloseKey(lRegKey)
    End If
 
    ' Launch the Browser passing the URL as parameter
    Shell sBrowser & " " & URL, ShowWindowState
End Sub
 
Public Sub ShellNewBrowser2(ByVal URL As String, _
    Optional ByVal ShowWindowState As VbAppWinStyle = vbNormalFocus)
    Dim sBrowser As String
    Dim iFile As Integer
 
    ' Create an Empty HTM file
    iFile = FreeFile
    Open "~Temp.htm" For Output As iFile
    Close iFile
 
    ' Use the "FindExecutable" API to return the path and name of the default browser
    sBrowser = Space(260)
    Call FindExecutable("~Temp.htm", "", ByVal sBrowser)
    sBrowser = Replace(Trim(sBrowser),  chr ( 0 ) , "")
 
    ' Remove the Temp file
    Kill "~Temp.htm"
 
    ' Launch the Browser passing the URL as a parameter
    Shell sBrowser & " " & URL, ShowWindowState
End Sub
Filed under: Internet Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.