Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ShellExecute hangs opening new browser window if default is Chrome or IE

I used this code (from MSDN) to open the default browser.

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

Private Sub Command1_Click()
   Dim r As Long
   r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)
End Sub

It works fine for IE and chrome only if an instance is already open, it then just ads a tab and returns the requested page.

If there is no instance already open a new one is created but the page never loads and eventually times out.

Only firefox seems to work OK in both scenarios.

I then tried a technique (detailed here) where I created a temporary htm file in order to find the associated application with a view to then using using one of the Shell calls below:

Shell "C:\Program Files\Internet Explorer\iexplore.exe https://www.google.co.uk/", vbNormalFocus

 Shell "C:\Program Files\Mozilla Firefox\firefox.exe https://www.google.co.uk/", vbNormalFocus

 Shell "C:\Users\Kjack\AppData\Local\Google\Chrome\Application\chrome.exe https://www.google.co.uk/", vbNormalFocus

This time both FF and IE worked but chrome still had the same problem.

Does anyone know a solution to this?

EDIT to add details about OS and browser versions :

OS : Windows Vista Home Basic 32 bit

FireFox 20.0.1

IE 9.0.8112.16421

Opera 12.15 Build 1748

Chrome 26.0.1410.64 m

like image 989
kjack Avatar asked Dec 01 '25 06:12

kjack


2 Answers

Since Win2k & Me arrived you also have a clean COM-based interface for this. It offers Unicode support without gyrations:

Shell.ShellExecute method

    With CreateObject("Shell.Application")
        .ShellExecute "http://www.microsoft.com"
    End With

That example lets all of the optional parameters (including the verb) default. As already pointed out in Ken White's answer you want the verb to default in this case.

like image 157
Bob77 Avatar answered Dec 04 '25 06:12

Bob77


Your code makes the assumption that there is an open verb assigned to the http: protocol, and there's no guarantee that it has been by the browser's installation.

Instead, just leave the verb empty. If you don't specify one, Windows will use whatever the default action is for the protocol or file association.

Private Sub Command1_Click()
   Dim r As Long
   r = ShellExecute(0, "", "http://www.microsoft.com", 0, 0, 1)
End Sub

Tip: You can see what the default action is for a file by right-clicking it in Windows Explorer. The default action is the one at the top of the context menu that's displayed in bold text.

like image 39
Ken White Avatar answered Dec 04 '25 06:12

Ken White