ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA Code== Goto a webpage using a command button in a UserForm (https://www.excelbanter.com/excel-programming/449166-vba-code%3D%3D-goto-webpage-using-command-button-userform.html)

[email protected]

VBA Code== Goto a webpage using a command button in a UserForm
 
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

GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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



GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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



Auric__

VBA Code== Goto a webpage using a command button in a UserForm
 
GS wrote:

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.

--
I know the human being and fish can coexist peacefully.
-- George W. Bush

GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
GS wrote:

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.


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



Auric__

VBA Code== Goto a webpage using a command button in a UserForm
 
GS wrote:

GS wrote:

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.


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.

--
Quick impression for you. Caw! Caw! Bang! ****, I'm dead!

GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
GS wrote:

GS wrote:

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.


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.


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



Harald Staff[_8_]

VBA Code== Goto a webpage using a command button in a UserForm
 
Just to mention an alternative:

Sub Click()
ActiveWorkbook.FollowHyperlink "https://www.google.com/"
End Sub

Best wishes Harald


skrev i melding
...
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




GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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



Michael Davis

VBA Code== Goto a webpage using a command button in a UserForm
 
On Saturday, August 17, 2013 4:53:26 AM UTC-4, Harald Staff wrote:
Just to mention an alternative:



Sub Click()

ActiveWorkbook.FollowHyperlink "https://www.google.com/"

End Sub

This worked perfectly. if this is the alternative, yet works so simply, why use the first method mentioned in the first post? Thanks for all the help!

Best wishes Harald





skrev i melding

...

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


Harald Staff[_8_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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

"Michael Davis" skrev i melding
...
On Saturday, August 17, 2013 4:53:26 AM UTC-4, Harald Staff wrote:
Just to mention an alternative:



Sub Click()

ActiveWorkbook.FollowHyperlink "https://www.google.com/"

End Sub

This worked perfectly. if this is the alternative, yet works so simply,
why use the first method mentioned in the first post? Thanks for all the
help!




GS[_2_]

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

ThisWorkbook.FollowHyperlink

--
Garry

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



GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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



Harald Staff[_8_]

VBA Code== Goto a webpage using a command button in a UserForm
 
Doh! I do much of this from addins, why haven't I thought of ThisWorkbook?
Thanks Garry!

"GS" skrev i melding ...
Surely...

ThisWorkbook.FollowHyperlink

--
Garry

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





GS[_2_]

VBA Code== Goto a webpage using a command button in a UserForm
 
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




All times are GMT +1. The time now is 04:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com