VBA Code== Goto a webpage using a command button in a UserForm

M

mdavis1154

I want to write a code so when I press a command button in a userform it will take me to a particular website.. Anyone know how to do this??

Thanks
 
G

GS

I want to write a code so when I press a command button in a userform it will
take me to a particular website.. Anyone know how to do this??

Thanks

You can do this using the ShellExecute API...


Put this in your Declaration section of the code module:
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


Put this in the macro that runs when you click the button:
ShellExecute 0&, vbNullString, "URL http info>" vbNullString,
vbNullString, vbNormalFocus

...where the full URL is passed as a string value in the 3rd arg. Note
that the macro should be in the same code module as the API declaration
because its scope is 'Private'. If you want several sheets to use the
same API then put it in a standard module and change its scope to
'Public'.

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

Oops, a typo...

ShellExecute 0&, vbNullString, "URL http info", vbNullString,
vbNullString, vbNormalFocus

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
A

Auric__

GS said:
Oops, a typo...

ShellExecute 0&, vbNullString, "URL http info", vbNullString,
vbNullString, vbNormalFocus

It should perhaps be noted that this will open the user's default browser.
 
G

GS

GS said:
It should perhaps be noted that this will open the user's default browser.

Yes, I assumed this to be the best idea (in all cases) but I agree with
you that I should have mentioned it!

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
A

Auric__

GS said:
Yes, I assumed this to be the best idea (in all cases) but I agree with
you that I should have mentioned it!

Maybe not in all cases. If you have, say, documentation written in HTML that
was only tested on MSIE, then you might want to ensure that your page opens
*only* in MSIE, regardless of the user's settings.
 
G

GS

GS said:
Maybe not in all cases. If you have, say, documentation written in HTML that
was only tested on MSIE, then you might want to ensure that your page opens
*only* in MSIE, regardless of the user's settings.

Well yes, I agree. In fact, I take this a step further in my apps that
use a HTML.EXE for a userguide in that all web links in the app open in
the EXE's browser by send it a simple command line if it's not running,
or via SendMessage if it's currently open.

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

Just to mention an alternative:
Sub Click()
ActiveWorkbook.FollowHyperlink "https://www.google.com/"
End Sub

But of course! And ever so simple, too! Thanks for the reminder that
this is a method available in most of the MSO suite. I've put so much
focus on building reusable generic code that I've forgotten some of the
more simple approaches to some tasks.<g>

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
H

Harald Staff

There are times when ActiveWorkbook is Nothing and the code fails, like when
no workbooks are open, or if the active one is in "protected mode" or
something like that, new stuff. The easy for the developer, but for the user
rather confusing, workaround is

If ActiveWorkbook Is Nothing Then
Workbooks.Add (1)
Doevents
End If
ActiveWorkbook.FollowHyperlink "https://www.google.com/"

... Garry's first method is a far smoother user experience at least in those
cases.

Best wishes Harald
 
G

GS

If anyone is interested in a reusable process that's not application
object dependant...

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 OpenUrl(Url$)
' Opens the specified URL in the user's default browser
ShellExecute 0&, vbNullString, _
Url, _
vbNullString, vbNullString, vbNormalFocus
End Sub

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
H

Harald Staff

Doh! I do much of this from addins, why haven't I thought of ThisWorkbook?
Thanks Garry!
 
G

GS

Doh! I do much of this from addins, why haven't I thought of
ThisWorkbook? Thanks Garry!

Ha, ha! That's why I opted to go with the generic VB approach using
ShellExecute. It works great when using COMAddins without having to
modify it for Excel, Word, or whatever target object ref required!

A bonus is that it's portable 'as is' to my VB6.EXE stand-alone
versions of my Excel addins. (I use the FarPoint fpSpread.ocx
spreadsheet control so users don't need MS Office installed!)

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top