PDA

View Full Version : Solved: Set FollowHyperlink default Web browser



Dave
04-17-2009, 02:36 PM
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

Dim strWebsite As String
strWebsite = "http://www.isthereanybodyoutthere.com/"
ActiveWorkbook.FollowHyperlink Address:=strWebsite, _
NewWindow:=True

edit: ps. just made up the link. I tried it and it looks interesting.

Dave
04-18-2009, 09:15 AM
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

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 (http://www.vbaexpress.com/)", 0, 0, 1)
End Sub



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

mdmackillop
04-18-2009, 10:04 AM
Thanks Dave for the solutions.

xluser2007
04-21-2009, 03:51 PM
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?

xluser2007
04-24-2009, 04:12 PM
Any ideas on the above queries guys?

xluser2007
04-26-2009, 04:48 AM
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:

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
Not sure how to maximise Dave's first code to open the defaul browser as MAXIMISED. Could anyone please assist with this?

Dave
04-27-2009, 05:27 AM
It seems like the "1" on the right end of this could be a 2 or 3? Dave
r = ShellExecute(0, "open", "http://www.vbaexpress.com (http://www.vbaexpress.com/)", 0, 0, 1)
ps. Thank you for posting your solution

xluser2007
04-27-2009, 04:55 PM
It seems like the "1" on the right end of this could be a 2 or 3? Dave
r = ShellExecute(0, "open", "http://www.vbaexpress.com (http://www.vbaexpress.com/)", 0, 0, 1) 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 :friends:!

I tried the following though based on your suggestion:



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
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?

Dave
04-28-2009, 05:33 AM
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

xluser2007
05-02-2009, 05:29 PM
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 :thumb), there is the code below:

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/Programming/Languages/Visual_Basic/Q_23225744.html", Show_Normal

End Sub
It is sourced from here (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_23225744.html) 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.