6

I'm trying to change the file association for opening URL's in Excel cell in another browser. According to the use of (CMD) ftype command with Excel.UriLink.16, I should be able to do this from powershell (via cmd) using:

# To change it:
cmd /c 'ftype Excel.UriLink.16=C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe "--single-argument" "%1"'

# The original is: 
# cmd /c ftype | findstr /i "excel.uri"
# Excel.UriLink.16=C:\Program Files\Microsoft Office\Root\Office16\protocolhandler.exe "%1"

This seem to have no effect at all, and will always open the URL in the default browser.

What is controlling the Excel opening of URLs and how to change this?


UPDATE: 2022-07-30

Following another SO here, state to check:

HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\*\UserChoice

With:

(Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\*\UserChoice -Name ProgId).ProgID | Where-Object -FilterScript{ $_ -like "*HTM*"}

Output:

FirefoxHTML-308046B0AF4A39CB
FirefoxHTML-308046B0AF4A39CB
MSEdgeHTM
MSEdgeHTM
MSEdgeHTM

Unfortunately not helpful.


I'm starting to understand better how this works,and it looks like this is deeply hidden in the OS. The seemingly undocumented program protocolhandler.exe is taking care of how all URI's are opened.

In addition the assoc show that .URL=InternetShortcut, is linked to:
InternetShortcut="C:\WINDOWS\system32\rundll32.exe" "C:\WINDOWS\system32\ieframe.dll",OpenURL %l.

Checking others, we find:

# (cmd /c ftype |Select-String protocol |Select-String excel ) -replace ("=","=`t")

Excel.UriLink.16=  C:\Program Files\Microsoft Office\Root\Office16\protocolhandler.exe "%1"
ms-excel=          C:\Program Files\Microsoft Office\Root\Office16\protocolhandler.exe "%1"

As shown in the MS Documentation on URI, uri-schemes and info on Excel Hyperlinks, this program need to be able to handle anything...


UPDATE: 2022-07-31

Apparently the visible usage of assoc and ftype are only smoke & mirrors for determining the File Type Associations (FTA). According to this SU answer, MS assoc XML and a security researchers blog, the way files are associated in Win10+ have completely changed since Win8, preventing user to take control of this by hashing associations to ensure users can't change them easily.

To query the file association XML, you can use:

# Run as Admin
Dism.exe /online /export-defaultappassociations:.\myCurrentFileAssoc.xml

...
<Association Identifier=".url" ProgId="IE.AssocFile.URL" />
...

# If there are any errors, you can find a LOG file in:
C:\WINDOWS\Logs\DISM\dism.log

The unofficial* tools exists to query and change these are:
(* Unofficial because MS doesn't want you to mock around with file associations.)

IMPORTANT:
SetUserFTA must be run in the users context, not with administrative or system privileges. Sometimes the timing can be important a swell – make sure it runs after the profile of the user is loaded.

Running it with:

# SetUserFTA.exe get | Select-String url

.url, IE.AssocFile.URL
http, FirefoxURL-308046B0AF4A39CB
https, FirefoxURL-308046B0AF4A39CB

# To see all installed browsers
# SetDefaultBrowser.exe

...
HKLM Brave
  name: Brave
  path: "C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe"
...

You can also associate portable browsers by following instructions here.

⛔ Still no progress for dealing with Excel assoc's...

not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • I used procmon to look at this and Excel never uses the `Excel.UriLink` at all. The first place it looks at to open a URL is in the `HKCU\SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice\ProgId` reg key but then it looks at literally dozens of other reg keys to validate the `UserChoice` before it actually opens the URL. Checking the `ftype http` on two of my systems, they both still associate to IExplore.exe even though the default browsers are Chrome and Firefox. – DBADon Jul 28 '22 at 22:23
  • I also exported the registry before, and after, manually changing my default browser and then I diffed the two. I then copied the dozens of registry changes and applied them in an attempt to revert the changes, this failed. I then tried going the other way by resetting my default browser and applying the other reg changes, this failed again. – DBADon Jul 28 '22 at 22:32
  • Does [this](https://stackoverflow.com/a/51727990/14903754) answer your question? – Abraham Zinala Jul 29 '22 at 20:16
  • @AbrahamZinala No, not really. Doesn't say how to apply the values to get URLs to open i different place. – not2qubit Jul 29 '22 at 23:40
  • 1
    how about [this](https://github.com/DanysysTeam/PS-SFTA) then?:) – Abraham Zinala Aug 01 '22 at 03:58

2 Answers2

2

This is not an answer, but may be helpful in similar situations.

Using VBA

The only way I managed to conditionally open a URL from an Excel cell with a different (than default) browser, was by using a VBA code snippet and adding it to the list of Microsoft Excel Objects. This have several drawbacks, but also one nice surprise.

Drawbacks:

  • Forces you to enable VBA macros in Excel
  • Forces you to change the Excel filetype from *.xlsx to *.xlsm

Surprise:

Long-Click on a cell can be used to separate the browser to be used to open a hyperlinked URL. This in combination with clicking on the hyperlink vs. in the blank part of the cell. In other words, a short click on the (matching) hyperlink will open the default browser, whereas a long click will open the URL in the (coded) browser.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    On Error Resume Next
    Dim Link As String
    Dim bravePath As String
    
    Link = Target.Value
    
    bravePath = """C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe"""

    SubString = "bing"
    
    If InStr(Link, SubString) <> 0 Then
        'if str contains 'bing'
        'Call OpenBrave
        'Application.Run "Module1.OpenBrave", Link
         Shell (bravePath & " -tab " & Link)
         
    'ElseIf InStr(Link, SubString) = 0 Then
        'if str does not contain it
        'Call OpenDefault
        'MsgBox "That is a normal URL", , "No Party Like This"
        'ActiveWorkbook.FollowHyperlink Address:=Link, NewWindow:=True, AddHistory:=True
        
    End If

End Sub

As you can see I commented out the ActiveWorkbook.FollowHyperlink (default bahaviour), as it is not needed, and can be replaced by another browser.

This is how it looks.

enter image description here

Again, to add the VBA code:

  • Save Excel sheet as *.xlsm type
  • enable VBA & Macros in settings
  • Left-click on sheet tab and select View Code
  • Paste the above code.

Done!

not2qubit
  • 14,531
  • 8
  • 95
  • 135
1

There is, unfortunately, no way that I know of to stop Excel from opening a valid hyperlink in the default program when you click on it. (i.e. the *FollowHyperlink event does not contain a Cancel option, unlike — for example — the Workbook_BeforeClose event)

However, it is possible to create a hyperlink that goes nowhere: rather than typing the URL in manually, use the Hyperlink editor (right-click in the cell, and select "Link" down at the bottom, or press Ctrl + K with the cell selected), go to the "Place in This Document" tab, and type the Cell's own reference in. This will give you a Hyperlink that takes you to the cell you are already in! However, you can set the Text to Display to be your target URL.

Then you can "catch" the Hyperlink Click in the *FollowHyperlink event (Either the Worksheet_FollowHyperlink event for a single Worksheet, or the Workbook_SheetFollowHyperlink event for the entire workbook), check if it "goes nowhere" — and, if so, navigate to the URL in the TextToDisplay with your preferred browser:

Option Explicit

Private Const PreferredBrowser As String = "C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe"

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh AS Object, ByVal Target AS Hyperlink)
    If Target.SubAddress = "" Then Exit Sub 'External Hyperlink
    If Range(Target.SubAddress).Address(True,True,xlA1,True) <> _
        Target.Range.Address(True,True,xlA1,True) Then Exit Sub 'Hyperlink is not recursive
    
    Shell """" & PreferredBrowser & """ -tab """ & Target.TextToDisplay & """"
End Sub

(Alternatively, you could create a "Shell.Application" object, grab the most recently opened window, check if that is an IE/Edge window the same URL as the hyperlink you just clicked on, and then try to force-close it while you open the hyperlink in your own browser…)

Chronocidal
  • 6,827
  • 1
  • 12
  • 26
  • Very interesting solution. Sorry for late response. I have since moved on, and did not have the chance to test this, yet. – not2qubit Nov 24 '22 at 13:43