Consulting

Results 1 to 10 of 10

Thread: Solved: Set FollowHyperlink default Web browser

  1. #1
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location

    Solved: Set FollowHyperlink default Web browser

    I'm using the following code to have an XL object open a web page. It works but opens 2 browsers with the same page (ie. firefox and IE. I would guess that it opens ALL browsers that are available). I'm looking to VBA an opening to IE only if anyone has any suggestions/code. Dave
    [vba]
    Dim strWebsite As String
    strWebsite = "http://www.Isthereanybodyoutthere.com/"
    ActiveWorkbook.FollowHyperlink Address:=strWebsite, _
    NewWindow:=True
    [/vba]
    edit: ps. just made up the link. I tried it and it looks interesting.

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    With abit more persistant googling, I was able to resolve this. Here are 2 separate approaches. The first is an API which uses only the default browser. The second uses the MS internet explorer. Dave
    [vba]
    Private Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long
    Sub Ainternet()
    Dim r As Long
    r = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 1)
    End Sub
    [/vba]

    [vba]
    Sub Binternet()
    Dim oIE As Object
    Set oIE = CreateObject("InternetExplorer.Application")
    oIE.Navigate ("http://www.vbaexpress.com")
    Set oIE = Nothing
    End Sub
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks Dave for the solutions.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    As the OP has answered their original quetion, I have a similar question:

    Is there a way to ensure that the browser window is set to MAXIMISE focus when opened?

    Also how could one open and then MINIMISE the focus?

  5. #5
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    Any ideas on the above queries guys?

  6. #6
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    I found the answer over at this link:

    http://www.mrexcel.com/forum/showthread.php?t=118049

    To modify Dave's code, I modified and tested the following and it worked:

    [vba]Option Explicit

    Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
    (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

    Public Enum apiShowWindowSize

    SW_MAXIMIZE = 3
    SW_SHOWNORMAL = 1
    SW_SHOWMINIMIZED = 2

    End Enum


    Sub test()

    Dim objIE As Object

    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Navigate ("http://www.vbaexpress.com")
    objIE.Visible = 1
    apiShowWindow objIE.hwnd, SW_MAXIMIZE


    End Sub[/vba]
    Not sure how to maximise Dave's first code to open the defaul browser as MAXIMISED. Could anyone please assist with this?

  7. #7
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    It seems like the "1" on the right end of this could be a 2 or 3? Dave
    [VBA]r = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 1)[/VBA]
    ps. Thank you for posting your solution

  8. #8
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    Quote Originally Posted by Dave
    It seems like the "1" on the right end of this could be a 2 or 3? Dave
    [vba]r = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 1)[/vba] ps. Thank you for posting your solution
    Hi Dave, no probs regrading my post, its great to contribute in any way on this collaborative forum, so thanks to you too !

    I tried the following though based on your suggestion:

    [vba]

    Private Declare Function ShellExecute _
    Lib "shell32.dll" _
    Alias "ShellExecuteA" ( _
    ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) _
    As Long

    Sub OPEN_Default_Internet()

    Dim r1 As Long
    Dim r2 As Long
    Dim r3 As Long

    r1 = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 1)

    r2 = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 2)

    r3 = ShellExecute(0, "open", "http://www.vbaexpress.com", 0, 0, 3)

    End Sub[/vba]
    They all OPEN as maximised, given this is my default browser i.e. firefox settings.

    As such, I don't believe my browser is responding to the numerical input.

    Did the above test work for you?

  9. #9
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    No the suggested change didn't do anything... but it didn't crash either. Your solution didn't change anything for me either? I'm using Vista (argh!) so I am used to having peculiar unexpected outcomes. Sorry I can't offer much further help. Dave

  10. #10
    VBAX Expert
    Joined
    Dec 2007
    Posts
    522
    Location
    Quote Originally Posted by Dave
    No the suggested change didn't do anything... but it didn't crash either. Your solution didn't change anything for me either? I'm using Vista (argh!) so I am used to having peculiar unexpected outcomes. Sorry I can't offer much further help. Dave
    Dave, that's fine mate. Actually I am using Vista (at home PC) and maybe having a the same issue due to that as well.

    I did test on XP and Excel 2003 and my default browser (firefox) opened as maximised even if I specified otherwise. So perhaps it is a firefox overwriding setting as well.

    By the way, I just found out that if you want to do your great ShellExecute method using Enums (for intellisense, thanks Bob ), there is the code below:

    [vba]Option Explicit

    Enum W32_Window_State
    Show_Normal = 1
    Show_Minimized = 2
    Show_Maximized = 3
    Show_Min_No_Active = 7
    Show_Default = 10
    End Enum

    Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hWnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

    Function OpenURL(URL As String, WindowState As W32_Window_State) As Boolean

    ' Opens passed URL with default application, or Error Code (<32) upon error

    Dim lngHWnd As Long
    Dim lngReturn As Long

    lngReturn = ShellExecute(lngHWnd, "open", URL, vbNullString, _
    vbNullString, WindowState)

    OpenURL = (lngReturn > 32)

    End Function

    Sub test()

    OpenURL "http://www.experts-exchange.com/Prog..._23225744.html", Show_Normal

    End Sub[/vba]
    It is sourced from here at Experts Exchange. It does not solve the problem directly but has the Enums make it easier to use.

    In order to solve the problem of opening the default browser in a particular focus though, Is it a case of using "Shell" instead of "ShellExecute", if so, how would this be modified to solve the problem of opening as maximised normal or minimised or otherwise?

    Could any experts please advise on this problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •