Home > How-To Library > Internet
How to open a new instance of default browser and browse to a specific file
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 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
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.