VBA – Determine Default Web Browser

Related to a recent question made to my VBA – Open a URL in FireFox/Chrome article and out of curiosity, I decided to figure out how one could determine which Web Browser was defined as the default web browser for a PC.

The Common Approach

I went out digging and the information I was finding simply didn’t work when I tested it. Some posts were mentioning checking the

HKEY_CLASSES_ROOT\http\shell\open\command\

and/or

HKEY_CLASSES_ROOT\https\shell\open\command\

registry key(s).

Some mentioned checking

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice

but as I said, the returned values didn’t match my settings when I’d make changes to my default app.

So I kept digging.

My Solution to Find the Default Web Browser

So what is one to do then?

I decided to profile my PC while I made changes to my Default Apps. From the data collected, I was able to determine that the actual registry key of interest was

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice\ProgId

from there, it then just became a question of building a wrapper function to read the value, cross reference that value to isolate the actual exe filename.

Below is my solution to identifying the default web browser on a PC:

'---------------------------------------------------------------------------------------
' 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
    'Trim off the exe and only return the filename
    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

Once again, the above code uses Late Binding techniques and has no application specific dependencies making it fully portable.

4 responses on “VBA – Determine Default Web Browser

  1. peter n roth

    off topic:
    Do you think a class-action lawsuit would stop MS from attempting to update my computer (and failing in every attempt)? I just want them to leave me alone!

    1. Daniel Pineault Post author

      Sorry Peter, but no one has ever managed to stop Microsoft, or any of the big IT monopolies from doing as they please. Windows 10 is the worst offender. The choice is to use alternatives, IMHO that mainly means Linux. I also know IT people that are still running Windows 7 just to not be using Windows 10 and I can’t blame them.

      Have you tried switching Update Channels to one that updates less frequently?

      When you consider the issues all users have endured with Windows 10, IMHO the most unstable version of Windows yet, and nothing has ever been done to Microsoft for the damages they have caused to 100s of thousands, millions of users. Heck some people lost the entire Documents folder, all their data and nothing! There is little to no recourse, read the EULA it is frightening, Microsoft has all the rights (even to all your data – files, folders, …) and the end-user none.

      Good luck.

  2. peter n roth

    *sigh*
    yes, the alternatives start to become more attractive.
    I’m even flirting with the thought … “Apple”. There, I said it.
    Linux, too, as both OSs have derived from similar sources…

    1. Daniel Pineault Post author

      I’m not a huge Apple fan, but I will say this, I bought my wife an iPad 6+ years ago and not once have we ever had any issues with it, not 1! The thing just works. No waiting for updates, no updates without my consent or in the middle of critical work (as has been the case for me with Windows 10), no blue screens of death, … Not sure about MacBooks…, but their iPad offerings would make me take a leap of faith and try them. But I’d first go with Linux personally, ubuntu is quite nice IMHO, try it in a VM.