Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Launch URL from Text or RichText box 1

Status
Not open for further replies.

TapeMan

Vendor
Feb 19, 2002
18
0
0
GB
I want to be able to launch a URL that is entered (or pasted) into either a text box or rich text box. The URL will be obtained from an e-mail, and will be the full URL complete with http: etc...

Is there an easy way to do this?

Thanks.
 
You can use the Shell() function.

A quick keyword search on this forum (for shell IE) turned up many examples including thread222-781887

Hope this helps

HarleyQuinn
---------------------------------
Help us to help you,
read FAQ222-2244 before posting.
 
Works a treat! Thanks very much.

For reference, I placed this code: lng = Shell("C:\PROGRAM FILES\INTERNET EXPLORER\IEXPLORE.EXE " & txtSRNum.Text, vbNormalFocus)

in the click event for the text box displaying the URL. Note that the space after .exe is important!
 
My full URL recoginition code for the RTB in thread222-878963 may be of interest
 
Hardcoding the path of iexplorer.exe can be dangerous and not recommended. Your code will fail to work if Windows is not installed on C: drive. You should either retrieve the correct path of iexplorer, or use the alternate method in thread222-1021403.
 
Instead of using Shell, I would suggest using Late Binding, which uses the Class name, rather than the path name...

Try this:
Code:
Sub ieLaunch(URL As String)
  With CreateObject("InternetExplorer.Application")
    .Visible = True
    .Navigate URL
  End With
End Sub

To use, simply pass the url string to the sub:
Code:
ieLaunch "www.google.com"

If you want to hold on to the Object, you could make it a funtion as well, with a slight modification:
Code:
[b]Function ieLaunch(URL As String) As Object
  Dim myIE As Object
  Set myIE = CreateObject("InternetExplorer.Application")[/b]
  With [b]myIE[/b]
    .Visible = True
    .Navigate URL
  End With
  [b]Set ieLaunch = myIE
End Function[/b]
Use:
Code:
ieLaunch "[URL unfurl="true"]www.google.com"[/URL]
'--OR--
Dim ieObj as Object
Set ieObj = ieLaunch("www.google.com")

This way, you can work with the document in the browser...

Here is a little example of different things you can do with this method...
Create New Project and add:
4 Command Buttons
1 Text Box (normal)
1 Text Box (MultiLine=True, ScrollBars=Both)

Then add the following code to the Form's code area:
Code:
Dim ieObj As Object

Private Sub Form_Load()
  Text1.Text = "[URL unfurl="true"]www.google.com"[/URL]
  If Not Text2.MultiLine Then MsgBox "Please set Text2's MultiLine property to true"
  If Text2.ScrollBars <> vbBoth Then MsgBox "Please set Text2's ScrollBars property to Both"
  Command1.Caption = "New Window"
  Command2.Caption = "Reuse Window"
  Command3.Caption = "Get Source"
  Command4.Caption = "Test Google"
End Sub

Private Sub Command1_Click()
  Set ieObj = ieLaunch(Text1.Text)
End Sub

Private Sub Command2_Click()
  ieChange Text1.Text, ieObj
End Sub

Private Sub Command3_Click()
  Text2 = ieGrabSource(ieObj)
End Sub

Private Sub Command4_Click()
  [b]'Insert search string into google and submit[/b]
  If Not ieObj Is Nothing Then
    ieObj.document.getelementbyid("q").Value = "tek-tips visual basic"
    Dim myTag As Object
    For Each myTag In ieObj.document.getElementsByTagName("input")
      If LCase(myTag.Type) = "submit" And InStr(1, LCase(myTag.Value), "search") Then myTag.Click
    Next
  End If
End Sub

[b]'Open new IE window and set URL
Function ieLaunch(URL As String) As Object
  Dim myIE As Object
  Set myIE = CreateObject("InternetExplorer.Application")
  With myIE
    .Visible = True
    .Navigate URL
  End With
  Set ieLaunch = myIE
End Function

'Change URL of existing window, or open new if one does not exist (during program session)
Sub ieChange(URL As String, myIE As Object)
  If Not myIE Is Nothing Then
    With myIE
      .Visible = True
      .Navigate URL
    End With
  Else
    Set myIE = ieLaunch(URL)
  End If
End Sub

'Return String containing the source for the Web Document
Function ieGrabSource(myIE As Object)
  If Not myIE Is Nothing Then
    ieGrabSource = myIE.document.documentElement.innerhtml
  End If
End Function[/b]

You could also place a WebBrowser control inside your project if you wanted to...
I do this sometimes to work with specialized Intranet programs...

strongm's suggestion is a good one, as I had stated in the refered thread, and is defitely worth a try.

I just wanted to provide a few other options with potential...

Hope This Helps,
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Sorry for the cross post, Hypetia, I started the thread before I went to lunch ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Shell Execute will open any fie in its default associated program, so if they aren't using IE, they won't get IE.
Code:
Option Explicit

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 Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1

Private Sub Command1_Click()
    ShellExecute 0&, "OPEN", "[URL unfurl="true"]www.google.com",[/URL] vbNullString, "C:\", SW_SHOWNORMAL
 ' c:\ = Destination Folder
End Sub

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top