Using VBA to open links in non-default browser

peddy00

New Member
Joined
Aug 19, 2012
Messages
27
Hi,

I have a pretty simple script to open the hyperlink in a cell:
Code:
Selection.Hyperlinks(1).Follow NewWindow:=False
My default browser is FireFox, but I'd like the script to open the link in Chrome? Can VBA do that?

Thanks for your help.

Peter
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
In a standard module:

Code:
Private Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal lpszPath As String) As Long
 
Private Const S_OK = &H0
Private Const S_FALSE = &H1
Private Const E_INVALIDARG = &H80070057
Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1

Public Function GetFolder(ByVal lngFolder As Long) As String

    Dim strBuffer As String * 1000
    Dim strPath As String
    Dim lngReturn As Long
    
    lngReturn = SHGetFolderPath(0&, lngFolder, 0&, SHGFP_TYPE_CURRENT, strBuffer)
    
    If lngReturn = S_OK Then
        strPath = Left$(strBuffer, InStr(strBuffer, Chr$(0)) - 1)
    Else
        strPath = "(error)"
    End If
    
    GetFolder = strPath
    
End Function

Sub ChromeLink()
Shell GetFolder(&H1C) & "\Google\Chrome\Application\chrome.exe " & Selection
End Sub
 
Upvote 0
Hi Scott,

Thanks for writing that out for me. I'm actually using a trigger, and have no experience with functions in VBA. If I insert the script for the function in a standard module, and then replace my command above with your command below, will that do the trick?
Code:
Shell GetFolder(&H1C) & "\Google\Chrome\Application\chrome.exe " & Selection
Thanks for the help.

Peter
 
Upvote 0
What do you mean a trigger? Do you mean event code, or is your code in a standard module as well?
 
Upvote 0
Sorry for being unclear, it's an event. When there's a change in the worksheet the script acts.
 
Upvote 0
Change the code in the standard module to this:

Code:
Public Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal lpszPath As String) As Long
 
Public Const S_OK = &H0
Public Const S_FALSE = &H1
Public Const E_INVALIDARG = &H80070057
Public Const SHGFP_TYPE_CURRENT = 0
Public Const SHGFP_TYPE_DEFAULT = 1

Public Function GetFolder(ByVal lngFolder As Long) As String

    Dim strBuffer As String * 1000
    Dim strPath As String
    Dim lngReturn As Long
    
    lngReturn = SHGetFolderPath(0&, lngFolder, 0&, SHGFP_TYPE_CURRENT, strBuffer)
    
    If lngReturn = S_OK Then
        strPath = Left$(strBuffer, InStr(strBuffer, Chr$(0)) - 1)
    Else
        strPath = "(error)"
    End If
    
    GetFolder = strPath
    
End Function
and if you are using Event code, you probably shouldn't need Selection, but I don't know exactly what you are doing, the following example is code that if you put a hyperlink in Column A, it will open it in Chrome:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Set c = Intersect(Range("A:A"), Target)
If c Is Nothing Then Exit Sub
If c.Cells.Count > 1 Then Exit Sub
If c.Hyperlinks.Count Then Shell GetFolder(&H1C) & "\Google\Chrome\Application\chrome.exe " & c
End Sub

[/code]
 
Last edited:
Upvote 0
Hi Experts companions as they solve to navigate Google Chrome is fabulous, try to do for Mozilla Firefox, but my little knowledge in how macros allow. I believe that is the statement "GetFolder" and on this I based. Have any of you dear friends could clarify my doubt? In advance thank you.
 
Upvote 0
Try this:

Code:
Private Declare Function SHGetFolderPath Lib "shell32.dll" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal lpszPath As String) As Long
 
Private Const S_OK = &H0
Private Const S_FALSE = &H1
Private Const E_INVALIDARG = &H80070057
Private Const SHGFP_TYPE_CURRENT = 0
Private Const SHGFP_TYPE_DEFAULT = 1

Public Function GetFolder(ByVal lngFolder As Long) As String

    Dim strBuffer As String * 1000
    Dim strPath As String
    Dim lngReturn As Long
    
    lngReturn = SHGetFolderPath(0&, lngFolder, 0&, SHGFP_TYPE_CURRENT, strBuffer)
    
    If lngReturn = S_OK Then
        strPath = Left$(strBuffer, InStr(strBuffer, Chr$(0)) - 1)
    Else
        strPath = "(error)"
    End If
    
    GetFolder = strPath
    
End Function

Sub FirefoxLink()
Shell GetFolder(&H26) & "\Mozilla Firefox\firefox.exe " & Selection
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top