Tuesday, February 1, 2011

Specifying a Browser in a Hyperlink

Specifying a Browser in a Hyperlink

Summary: Excel allows you to easily add hyperlinks to a worksheet. Click on it, and the target of the link is opened in a browser window. If you want to specify which browser is used to display the link, things get more complex. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Specifying a Browser in a Hyperlink.)

Sunny wants to include a hyperlink in a worksheet. However, he would like the hyperlink to "force" the target of the URL to be displayed in a particular browser. For instance, he would like the hyperlink to somehow specify that the target be opened in Internet Explorer.

There is no way to do this within Excel; a hyperlink in a worksheet, when clicked, relies on whatever the default browser is on the system being used. There is a workaround that you can try, however: You could create a macro that actually opens a target address using a specific browser.

For example, consider the following macro. It automatically opens an instance of Internet Explorer and opens my blog in that browser:

Sub LaunchIE()
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.navigate "
http://naveekuma.blogspot.com/"
    IE.Visible = True
    Set IE = Nothing
End Sub

The macro could easily be assigned to a shortcut or to a toolbar button. It isn't terribly flexible, however, when it comes to which browser is being used (it is always Internet Explorer) and which site is displayed (it is always the ExcelTips site). You can make it a bit more flexible in this manner:

Sub showURL(browser As String, URL As String)
    Dim pPath As String
    Dim bPath As String
    'Use this to resolve the correct program file path
    'it is different on 32-bit and 64-bit systems
    pPath = Environ("ProgramFiles")
    If browser = "Firefox" Then
        bPath = pPath & "\Mozilla Firefox\Firefox.exe"
    ElseIf browser = "IE" Then
        bPath = pPath & "\Internet Explorer\iexplore.exe"
    Else
        Exit Sub
    End If
    Call Shell(bPath & " " & URL, vbNormalFocus)
End Sub
Sub Testing()
    Call showURL("Firefox", "
http://msofficeadvise.blogspot.com/")
    Call showURL("IE", "
http://naveekuma.blogspot.com/")
End Sub

Note that the main routine—showURL, the one that does all the work—can work with either Internet Explorer or Firefox. The Testing routine shows how to launch the browsers; all you need to do is specify which browser you want and what URL you want to open in that browser.

MS Office Tips is your source for effective Microsoft Excel learning. This tip applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003


Tags: Browser in a Hyperlink, add hyperlinks to a worksheet, include a hyperlink in a worksheet

No comments:

Post a Comment