I need to know how to add a web hyperlink in my project
ie to open a web browser at a particular site.
thankyou
Printable View
I need to know how to add a web hyperlink in my project
ie to open a web browser at a particular site.
thankyou
Add these subs:
Then you call them like this:Code:Public Sub gotoweb(URL As String)
Dim Success As Long
Success = ShellExecute(0&, vbNullString, URL, vbNullString, "C:\", SW_SHOWNORMAL)
End Sub
Public Sub sendemail(Email As String)
Dim Success As Long
Success = ShellExecute(0&, vbNullString, "mailto:" & Email, vbNullString, "C:\", SW_SHOWNORMAL)
End Sub
Hope this helps!Code:Private Sub LblURL_Click()
gotoweb("http://www.your-homepage-here.com")
End Sub
Private Sub LblEmail_Click()
sendemail("[email protected]")
End Sub
I forgot, you also have to insert this code:
Code:Public 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
Public Const SW_SHOWNORMAL = 1
Damn! beat me to it!
The advantage of this is that it's not reliant on MS Internet explorer. It uses the default browser. I'm almost finished an OCX using this on a label where you can set:
Hyperlink1.Caption = "VB World"
Hyperlink1.Link = "Http://www.vb-world.net/"
(Just thought I'd take a moment to pat myself on the back :D)
Success = ShellExecute(0&, vbNullString, URL, vbNullString, "C:\", SW_SHOWNORMAL)
isn't the "C:\" slightly dangerous? What if there is no C:\ drive on the system? Say the 1st drive starts with D:\.
I am new to VB but this looks slightly dangerous. IS there an alternative method ?
The lpDirectory parameter that Zaphod64831 suggests that you pass "C:\" to is only the preferred working directory for the application.
Even if C:\ doesn't exist you wouldn't get any errors the app (or Windows) would just designate an other working directory. You can also pass vbNullString to this parameter if you like.
And by the way, since others take there time to pat there back I thought I would do the same :) Please read my tip (and download the demo) on how to add hyperlink capability to a RichTextBox.
Best regards
Most likely the users main drive will be C:\. But if you'd rather have the user tell you what drive they want:
You should ask that first, than save it to a file so the user doesn't have to answer again. Read/Write to an ini file for that. And no, I don't think calling C:\ will hurt you at all.Code:x = InputBox("What is your default drive?",,"C:\")
If x = "" Then Exit Sub
Success = ShellExecute(0&, vbNullString, URL, vbNullString, "" & x & "", SW_SHOWNORMAL)
I see what you mean. And jsut for clarification .. "think" as in the code will still work or as in most uses have a c:\ drive ?Quote:
You should ask that first, than save it to a file so the user doesn't have to answer again. Read/Write to an ini file for that. And no, I don't think calling C:\ will hurt you at all. [/B]
-jfs
Correction: I know it won't hurt you.
The only thing that could probably hurt you is if you kill/delete any important files on C drive.
Using ShellExecute will not harm you in any way. If you would rather have it so that the user doesn't get an error, if any should occur. Then do this:
That will ignore any errors completely. You are thinking of something bad happening. Think good thoughts and leave it the way it is! Most of your users will have C drive as there main drive. If they don't than just tell them to email you so you could edit the program and resend it.Code:On Error Resume Next
Success = ShellExecute(0&, vbNullString, URL, vbNullString, "C:\", SW_SHOWNORMAL)