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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a browser and send a URL string to it. 2

Status
Not open for further replies.

MrMajik

IS-IT--Management
Joined
Apr 2, 2002
Messages
267
I created a VB6 application that builds a URL for a browser. I can copy and paste the URL to the URL Address text box in a browser window and it works like a charm. However, I am stumped on how to get my VB6 application to open the default browser on a computer and pass the URL string to the browser and have the browser open with the web site loaded.

Can you offer any suggestions?

Thank you.
 

Dim objOpenBrowser As clsOpenBrowser
Set objOpenBrowser = New clsOpenBrowser
objOpenBrowser.OpenBrowser Me.hWnd, strLink


'Add to New Class Module and call it clsOpenBrowser
Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias _
"FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As _
String, ByVal lpResult As String) As Long
Const SW_SHOWNORMAL = 1
Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&
Public Sub OpenBrowser(ByVal hWnd As Long, ByVal strInternetAddress As String)
Dim FileName, Dummy As String
Dim BrowserExec As String * 255
Dim retVal As Long
Dim FileNumber As Integer
' First, create a known, temporary HTML file
BrowserExec = Space(255)
FileName = App.Path & "\temphtm1.HTM"
FileNumber = FreeFile
Open FileName For Output As #FileNumber
Write #FileNumber, &quot;<HTML> <\HTML>&quot;
Close #FileNumber
' Then find the application associated with it
retVal = FindExecutable(FileName, Dummy, BrowserExec)
BrowserExec = Trim(BrowserExec)
' If an application is found, launch it!
If retVal <= 32 Or IsEmpty(BrowserExec) Then ' Error
MsgBox &quot;Could not find associated Browser&quot;, vbExclamation, _
&quot;Browser Not Found&quot;
Else
retVal = ShellExecute(hWnd, &quot;open&quot;, BrowserExec, _
strInternetAddress, Dummy, SW_SHOWNORMAL)
If retVal <= 32 Then
MsgBox &quot;Web Page not Opened&quot;, vbExclamation, &quot;URL Failed&quot;
End If
End If
Kill FileName
End Sub
 
Try this.

Option Explicit
Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (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 Sub GoToURL()
Dim Success As Long
Dim strURL As String

strURL = &quot;Your URL&quot;
Success = ShellExecute(0&, vbNullString, strURL, vbNullString, &quot;C:\&quot;, SW_SHOWNORMAL)

End Sub
 
Oooops, forgot something.

Private Const SW_SHOWNORMAL = 1
 
Wow Thanks to DrJavaJoe and Click Here for such a fast reply!

I used the suggestion by Click Here because it is the fastest one to try first and it works flawlessly :)

MrMajik
 
DrJavaJoe; I revisited this issue and am going to use your code on my current project. I apoligize for neglecting to give you a well-deserved star for your solution. You have it now.

Thank you! :-)

MrMajik
 
DrJavaJoe; Can you send me an email? My address is:

greinesm@michigan.gov

I am having a bit of an issue or two getting your code example to work and was wondering if you could throw together a simple form with a button that, when clicked, would take you to and send me the code. Nothing fancy, please :-)

Thank you!
MrMajik
 
MrMajik, I'd suggest you consider ClickHere's somewhat easier solution.
 
DrJavaJoe; Thank you so much for taking the time for this. Your project code is identical to what I was trying with the exception of a typo in mine when I named the class module while creating a test project...

Your code does exactly what I was looking for...open a new browser window each time it is accessed.

You be da man [thumbsup]

MrMajik
 
No, I'm still confused as to why you prefer DrJavaJoe's code to ClickHere's. Java's has a whole bunch of completely unnecessary code, namely the whole block that exist merely to find the executable associated with an HTM file. ShellExecute will do this for you.

ClickHere's code can be reduced to (giving it the same function name as the competitor):
[tt]
Option Explicit

Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (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_SHOWNORMAL = 1

Private Sub OpenBrowser(strURL as string)
ShellExecute Me.hwnd, vbNullString, strURL, vbNullString, vbNullString, SW_SHOWNORMAL
End Sub
[/tt]

And has the advantage that you can pass it web sites, ftp sites, a folder path, a file path...

 
Hi Strongm;

I like both of the examples and am using both of them. The difference is DrJavaJoe's code opens up a new browser window while ClickHere's code loads the website into the last browser window a user used. This is handy for some situations while other situations a user may want to keep what is alreay in a browser window and open a new browser window.

More specifically, I am developing something that opens stock market charts and want to have more than one window open at a time. ClickHere's code will only open a new window if one is not open, otherwise it uses an existing window.

I will take a look at your code, too.

This place is awesome! I now have three different versions to add to my toolbox of reusable code. Thank you [thumbsup2]

MrMajik
 
My code is merely a minor modification to Clickhere's, not a new solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top