2

I have an Excel sheet with URLs that I want to open using a separate browser. I know how to set the default browser used by different apps using the windows built-ins:

cmd /c assoc
cmd /c ftype

This would display the (relevant) values as:

    
    Value Data  Browser
    -----------------------------------------
    BraveHTML                       Brave
    ChromeHTML                      Chrome
    FirefoxHTML-308046B0AF4A39CB    Firefox
    IE.HTTP                         Internet Explorer
    MSEdgeHTM                       EDGE
    -----------------------------------------

You can also access this info with powershell using:

# For HTTP
Get-ItemProperty HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice -Name ProgId

For HTTPS

Get-ItemProperty HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice -Name ProgId

For Excel we have:

    Excel.UriLink.16=C:\Program Files\Microsoft Office\Root\Office16\protocolhandler.exe "%1"
    Excelhtmlfile="C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE"

The problem here is the protocolhandler.exe which also handles internal references and who knows what else. For URLs (hyperlinks), it passes them to the default browser.

I want to wrap this to only pass HTTP URL links to the Brave browser (for example.)

How can I wrap this command to do the above and with an easy way to revert once I'm done?


Not helpful similar questions:


not2qubit
  • 2,651
  • 4
  • 34
  • 45

3 Answers3

0

Per gns100's comment you can use a VBA procedure to open URL's in a chosen web browser.

There are browser-specific scripts available on the web however Daniel Pineault's VBA procedure posted on DEVelopers HUT supports directing URLs to one of 6 browsers: Brave, Internet Explorer, Firefox, Chrome, Opera, and Edge. You can alternate the browser on a per URL basis within thew same document based on the call used.

Source: A Procedure to Control Them All

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(&quot;WScript.Shell&quot;)
'Default ProgId
sProgId = oShell.RegRead(&quot;HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations&quot; &amp; _
                         &quot;\UrlAssociations\https\UserChoice\ProgId&quot;)
'Cross-reference the sProgId to get the exe associated with it
sCommand = oShell.RegRead(&quot;HKEY_CLASSES_ROOT\&quot; &amp; sProgId &amp; &quot;\shell\open\command\&quot;)
'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), &quot;\&quot;))    ' firefox.exe
GetDefaultBrowser = Left(GetDefaultBrowser, InStr(GetDefaultBrowser, &quot;.&quot;) - 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 &quot;iexplore&quot;
        GetBrowserNameEnumValue = BrowserName.iexplore
    Case &quot;firefox&quot;
        GetBrowserNameEnumValue = BrowserName.firefox
    Case &quot;chrome&quot;
        GetBrowserNameEnumValue = BrowserName.chrome
    Case &quot;opera&quot;
        GetBrowserNameEnumValue = BrowserName.opera
    Case &quot;msedge&quot;
        GetBrowserNameEnumValue = BrowserName.msedge
    Case &quot;brave&quot;
        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

Blindspots
  • 3,472
-1

Based on my understanding, Excel's .xlsx file format does not support calling external commands. How about an alternative way by directing hyperlinks in Excel to Windows shortcuts (.lnk or .url files)?

  1. Create a Windows shortcut to Brave or Chrome program
    "C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe"
  2. Edit the shortcut's "Target" field and append the target URL
    "C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe" www.yahoo.com
  3. Create an Excel HYPERLINK formula that points to the full path of the shortcut instead of the URL
    =HYPERLINK("C:\Path\To\Shortcut\shortcut_name.lnk")

This method works without any VBA coding and doesn't trigger Antivirus warnings. Also, no security warnings are triggered when opening the Excel file. It is much easier compared to changing the file extension or creating a batch file.

The disadvantage of this approach is you need to separately create and maintain a shortcut file for each URL. Any changes to a URL will require manually editing the associated shortcut's target field.

Blindspots
  • 3,472
Den
  • 1
-2

This is fairly simple using Powershell and the ImportExcel module: In my example my spreadsheet has three columns, DATE, URL, OTHER_INFO and the URL column has the links.

Install the ImportExcel module. From the module, use the Import-Excel cmdlet to read the spreadsheet into a variable and then use a foreach loop to open each link:

$excel = Import-Excel -Path "C:\Users\UserName\Desktop\Temp\test.xlsx"
$brows = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
foreach ($url in $excel.URL){
    Start-Process -FilePath $brows -ArgumentList $url
    #    Write-Host $brows $url  ## TEST
    }

Start-Process -FilePath $brows -ArgumentList $url

This will open each link in a new tab in MS's Edge.

If you are unable to use the ImportExcel module, it is more verbose but you can still access spreadsheet data using the COM objects

In the example, replace the path to the browser with the path to whatever browser you want to use. (Note: I am using the full path to MS's Edge as I don't use the Brave browser.)

This has the added benefit of not needing to revert changes.

DBADon
  • 503