Another request we see in various forums is how to launch a URL in an alternate web browser and not use Internet Explorer or whatever the default browser happens to be on a given PC.
Automating FireFox
Luckily for us, FireFox accepts command line switches for such automation.
Below is a simple procedure that accepts the URL you wish to open and opens a new tab in FireFox to that URL.
'---------------------------------------------------------------------------------------
' Procedure : OpenURLInFF
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open a URL in FireFox
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL : URL to open in FifeFox
'
' Usage:
' ~~~~~~
' Call OpenURLInFF("http://www.google.ca")
' Call OpenURLInFF("devhut.net")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-11-13 Initial Release
' 2 2018-02-01 Updated Copyright under CC licensing
' Error trapped FireFox not installed
'---------------------------------------------------------------------------------------
Sub OpenURLInFF(ByVal sURL As String)
On Error GoTo Error_Handler
Dim WSHShell As Object
Dim sFFExe As String 'FF executable path/filename
'Determine the Path to FF executable
Set WSHShell = CreateObject("WScript.Shell")
sFFExe = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Firefox.EXE\")
'Open the URL
Shell """" & sFFExe & """" & " -new-tab """ & sURL & "", vbHide
Error_Handler_Exit:
On Error Resume Next
If Not WSHShell Is Nothing Then Set WSHShell = Nothing
Exit Sub
Error_Handler:
If Err.Number = -2147024894 Then
MsgBox "FireFox does not appear to be installed on this compter", _
vbInformation Or vbOKOnly, "Unable to open the requested URL"
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: OpenURLInFF" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Sub
Using Chrome Instead
Chrome can similarly be automate but instead of -new-tab, it is simply -url
A Procedure to Control Them All!
Okay, it was bothering me to have to have an individual VBA procedure for each browser and thought to myself: “How hard can it be to have a single procedure to use any browser of my choosing?”. Well, it turns out that it isn’t that hard at all (noting a minor special case for Opera and Microsoft Edge)! Below is a simple procedure that, currently, will work with 6 of the most popular browsers.
- Internet Explorer
- FireFox
- Chrome
- Opera
- Microsoft Edge
- Brave
Enum BrowserName
'This Enum is part of Sub OpenURL()
' *** If changes are made here, update GetBrowserNameEnumValue()
iexplore = 1
firefox = 2
chrome = 3
opera = 4
msedge = 5
brave = 6
End Enum
'---------------------------------------------------------------------------------------
' Procedure : OpenURL
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Open a URL in a browser
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
' Dependencies: BrowserName Enum, GetDefaultBrowser(), GetBrowserNameEnumValue()
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL : URL to open
' lBrowser : Optional, browser to be used to open the URL, if omitted, the system's
' default browser will be used
'
' Usage:
' ~~~~~~
' Call OpenURL("https://www.google.ca") 'will use the user's default browser
' Call OpenURL("https://www.google.ca", iexplore)
' Call OpenURL("devhut.net", chrome)
' Call OpenURL("msdn.com", firefox)
' Call OpenURL("google.ca", opera)
' Call OpenURL("https://www.google.ca", msedge)
' Call OpenURL("https://www.google.ca", brave)
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2014-11-13 Initial Release
' 2 2018-02-01 Updated Copyright under CC licensing
' Error trapped FireFox not installed
' 3 2018-02-01 Complete revamp of the code to accomodate multiple
' Browser
' 4 2020-04-27 Added Microsoft Edge
' Added Brave
' 5 2020-12-14 Adapted to now have lBrowser as optional and the
' ability to determine the system's default browser
' 6 2022-07-03 Fixed usage examples to match Enum, forgot to do so
' after the last update
' changed msedge sExe to make people happy, not used!
'---------------------------------------------------------------------------------------
Sub OpenURL(ByVal sURL As String, Optional lBrowser As BrowserName)
Dim oShell As Object
Dim sFFExe As String 'Executable path/filename
Dim sProgName As String 'Name of the Executable program
Dim sExe As String 'Executable exe filename
Dim sCmdLineSwitch As String 'Command line switch
Dim sShellCmd As String 'Shell Command
On Error GoTo Error_Handler
'If no browser is specified then use the system's default one
If lBrowser = 0 Then
lBrowser = GetBrowserNameEnumValue(GetDefaultBrowser())
End If
'Determine the Path to executable
Select Case lBrowser
Case 1
'https://msdn.microsoft.com/en-us/library/hh826025(v=vs.85).aspx
sProgName = "Internet Explorer"
sExe = "IEXPLORE.EXE"
sCmdLineSwitch = " "
Case 2
'https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options#Browser
sProgName = "Mozilla Firefox"
sExe = "Firefox.EXE"
sCmdLineSwitch = " -new-tab "
Case 3
sProgName = "Google Chrome"
sExe = "Chrome.exe"
sCmdLineSwitch = " -tab "
Case 4
'http://www.opera.com/docs/switches/
sProgName = "Opera"
sExe = "opera.exe"
sCmdLineSwitch = " "
Case 5
sProgName = "Microsoft Edge"
sExe = "msedge.exe"
sCmdLineSwitch = " -tab "
Case 6
sProgName = "Brave"
sExe = "brave.exe"
sCmdLineSwitch = " -tab "
End Select
If lBrowser = 5 Then 'Special case for Edge! Thank you Microsoft for not following the rules!
'Build the command
sShellCmd = "cmd /c """ & "start microsoft-edge:" & sURL & """"
Else
Set oShell = CreateObject("WScript.Shell")
sFFExe = oShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" & _
"CurrentVersion\App Paths\" & sExe & "\")
'Parse the returned string
sFFExe = Replace(sFFExe, Chr(34), "") 'Special case for Opera?!
'Build the command
sShellCmd = """" & sFFExe & """" & "" & sCmdLineSwitch & """" & sURL & """"
End If
'Open the URL
Shell sShellCmd, vbHide
Error_Handler_Exit:
On Error Resume Next
If Not oShell Is Nothing Then Set oShell = Nothing
Exit Sub
Error_Handler:
If Err.Number = -2147024894 Then
MsgBox sProgName & " does not appear to be installed on this compter", _
vbInformation Or vbOKOnly, "Unable to open the requested URL"
Else
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: OpenURL" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
End If
Resume Error_Handler_Exit
End Sub
'---------------------------------------------------------------------------------------
' Procedure : GetDefaultBrowser
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Returns the name of the System's Default Web Browser
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Usage:
' ~~~~~~
' GetDefaultBrowser()
' -> msedge, firefox, brave, iexplore, ...
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2020-12-14 Initial Release
'---------------------------------------------------------------------------------------
Function GetDefaultBrowser() As String
Dim oShell As Object
Dim sProgId As String
Dim sCommand As String
Dim aCommand As Variant
On Error GoTo Error_Handler
Set oShell = CreateObject("WScript.Shell")
'Default ProgId
sProgId = oShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations" & _
"\UrlAssociations\https\UserChoice\ProgId")
'Cross-reference the sProgId to get the exe associated with it
sCommand = oShell.RegRead("HKEY_CLASSES_ROOT\" & sProgId & "\shell\open\command\")
'Parse the returned value to extract just the exe filename
aCommand = Split(sCommand, Chr(34))
GetDefaultBrowser = Right(aCommand(1), Len(aCommand(1)) - InStrRev(aCommand(1), "\")) ' firefox.exe
GetDefaultBrowser = Left(GetDefaultBrowser, InStr(GetDefaultBrowser, ".") - 1) 'firefox
Error_Handler_Exit:
On Error Resume Next
If Not oShell Is Nothing Then Set oShell = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetDefaultBrowser" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
'---------------------------------------------------------------------------------------
' Procedure : GetBrowserNameEnumValue
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Convert the returned value from GetDefaultBrowser() into the proper Enum
' Value. This is required as VBA offers no way to evaluate a returned
' value from a function against an Enum, no way to iterate over the string
' values of an Enum, ...
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Usage:
' ~~~~~~
' GetBrowserNameEnumValue(GetDefaultBrowser())
' -> 1, 2, 3, ...
' GetBrowserNameEnumValue("firefox")
' -> 2
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2020-12-14 Initial Release
'---------------------------------------------------------------------------------------
Function GetBrowserNameEnumValue(sInput As String) As Long
On Error GoTo Error_Handler
Select Case sInput
Case "iexplore"
GetBrowserNameEnumValue = BrowserName.iexplore
Case "firefox"
GetBrowserNameEnumValue = BrowserName.firefox
Case "chrome"
GetBrowserNameEnumValue = BrowserName.chrome
Case "opera"
GetBrowserNameEnumValue = BrowserName.opera
Case "msedge"
GetBrowserNameEnumValue = BrowserName.msedge
Case "brave"
GetBrowserNameEnumValue = BrowserName.brave
Case Else
GetBrowserNameEnumValue = 0
End Select
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: GetBrowserNameEnumValue" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Dont know if this is possible but can I scrape some text from the opened firefox page.
VBA’s web browser automation is limited to Internet Explorer. That said, I had come across a project called Selenium which was supposed to support automating other browser, you could check it out at https://florentbr.github.io/SeleniumBasic/
Great code work – appreciate finding this – thank you for your efforts.
Just one comment – you Chrome code uses “-tab” but your article says..
“Chrome can similarly be automate but instead of -new-tab, it is simply -url”
This is amazing, thanks for this!
Hello Daniel !
Your OpenURL works fine.
And you had to add this new case for Edge and Windows 10 (that I dislike as much as you do!)
But I understand that your OpenURL does not identify which browser is installed as default on our users’ machines, am I wrong?
I checked many forum but I cannot find any satisfying method for retrieving the default browser…
Could you please help me out ?
Many thanks in advance !
phil
I’ve seen people mention to verify the value of
but, I didn’t find it to be good, in my testing, I’ve found the following to be the right key
AND/OR
So you can do something like
CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindowsShellAssociationsUrlAssociationshttpsUserChoiceProgId")which in turn returns values like:
IE.HTTP -> Internet Explorer
MSEdgeHTM -> Microsoft Edge
FirefoxHTML-308046B0AF4A39CB -> Mozilla Firefox
BraveHTML -> Brave
This seems to get the Settings -> Default apps -> Web browser setting, but don’t forget users can override individual extensions.
Taking things further, you can then, if need be, take the returned ProgId and get the exe by doing something like
CreateObject("WScript.Shell").RegRead("HKEY_CLASSES_ROOTTheRetunedProgIdFromAboveshellopencommand")So
CreateObject("WScript.Shell").RegRead("HKEY_CLASSES_ROOTFirefoxHTML-308046B0AF4A39CBshellopencommand")and this returns something like
That you can parse and extract whatever you need.
Hope this helps.
Here, I decided to try and create a quick demo of how it can all be assembled. It would need error handling …, but this would be the basic idea
Function GetDefaultBrowser() As String Dim oShell As Object Dim sProgId As String Dim sCommand As String Dim aCommand As Variant Set oShell = CreateObject("WScript.Shell") sProgId = oShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice\ProgId") sCommand = oShell.RegRead("HKEY_CLASSES_ROOT\" & sProgId & "\shell\open\command\") aCommand = Split(sCommand, Chr(34)) GetDefaultBrowser = Right(aCommand(1), Len(aCommand(1)) - InStrRev(aCommand(1), "\")) ' firefox.exe GetDefaultBrowser = Left(GetDefaultBrowser, InStr(GetDefaultBrowser, ".") - 1) 'firefox If Not oShell Is Nothing Then Set oShell = Nothing End Functionand it returns
Your comment got me curious. If you check the page again, I’ve completely updated the second function and now, if no browser is specified, it will default to the system’s default browser. This should make the function completely versatile for all use cases.
Thanks, sir for your valuable information. please advise me how to press ctrl +f with specific text then click on the selected text
Hi Daniel,
Appreciate all that you do.
I’m not sure how to modify this script to do exactly what I want, but I’m sure it’s rather easy for you to do.
I’m writing a VBA script to open a website, but the website does not work in IE, but does work in Chrome, Edge, or Firefox (we don’t use Brave or Opera).
So I want a script that checks if IE is the default browser, and if not, opens the website in the default browser and if so, tries to open in Firefox, Chrome, Edge, and then prompts the user, so basically, I want Error Number -2147024894 to return a failure rather than a MsgBox.
Rough coding:
If GetDefaultBrowser = “iexplore” Then
Call OpenURL(“somesite.com”, Firefox)
On Error Call OpenURL(“somesite.com, Chrome)
On Error Call OpenURL(“somesite.com, Edge)
On Error MsgBox “Internet Explorer is not supported by somesite.com. Please install Firefox or Chrome or Edge.”
Else
‘open in default browser
Call OpenURL(“somesite.com”)
EndIf
Thank you for any assistance.
If I’m using this method to open a CSV link is there a way to direct that to open into an existing workbook?
First of all, this is incredible. Thank you so much. It runs so cleanly. I am not sure if you are still toying with this.
Would it be possible to check first if the target tab is open (perhaps by searching for a partial text string for the tab title) to activate that if already open, then if no
Great
Bravo, bravissimo!
Thank you guy!
Daniel,
Is this correct?
Case 5
sProgName = “Microsoft Edge”
sExe = “Chrome.exe”
sCmdLineSwitch = ” -tab “
Try it and you’ll see it works just fine.
Hello Daniel,
Thank you for your effective and cleverly named. “Procedure to Rule them All”. I have pared it down to open Brave, , and it works, but I don’t know how to navigate to a website from there. Other code I’ve seen for navigating to a given url sets an object as the browser, for example, setting “ie” as “internet explorer” and then uses “ie.navigate”. I don’t see a similar definition in this code.
Thank you.
But the procedure will navigate you to the specified URL, so I don’t quite understand the question.
As for automation, only Internet Explorer has ever offered this ability natively. It has never been possible with Edge, FireFox, Brave, … as they don’t expose any code for us to hook into and use. That said, where there’s a will there’s a way, as they say! You can in fact automate other browser by installing and using the Selenium project: https://florentbr.github.io/SeleniumBasic/. Hope this helps.
This is crazy))) Thank you very much