How to Open a Url or Link in Default WebBrowser – VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 3.80 out of 5)
 

I figured this would be very easy to do since VB has a Shell command built-in. But I would keep getting a File not Found message everytime I would try and open a web address. It would work for other tasks like shelling “shutdown -r -f -t 0” which is a process that can be be used to shutdown or restart the computer, but not opening a URL. So I decided to go ahead and use the Windows APIs to do this task. Below is the API Call and codes to open a url in the default web browser.

____________________________________

This Function will be used to process the url which will have windows open the web browser pointing to that url path.

    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

Now all thats needed is to execute that function passing the url you want opened in the third parameter. For this task the 3rd parameter is the most important. Well, I guess 2, 3, 6 should be set. Parameter #2 is how to process the task in the 3rd parameter which will be set to “open”. You could just put a blank string like “” which will still open the url. But just enter “open” to be on the safe side. Parameter #6 is the window style to open the application. Like 1 is for normal and such. As far as I know the other paramters is not required for this to work so I just set them to 0 or vbnullstring.

        '
        'In the last parameter you can specify the program window style.
        '0 = Hide, 1 = Normal, 2 = Minimize, or 3 = Maximize.
        '
        'For other styles just open the VB API Viewer application under your VB's Tools directory and open the Win32Api.txt file and check out the constants that starts with "SW" like SW_NORMAL.
        '
        'Anyways, this will have the Windows Shell open the Default WebBrowser with the url specified in the 3rd parameter.
        '
        ShellExecute 0, "open", "http://www.vbcodesource.info", vbNullString, vbNullString, 1

____________________________________

I added the next codes just to show how to open another program and pass arguments to the program. This code will launch the FireFox webbrowser and open the URL in the 4th parameter.

 

        '
        'This simply shows how to open a specific program like Firefox which may not be the default webbrowser
        'and how to get it to go to a specific url.
        ShellExecute 0, "open", "C:\Program Files\Mozilla Firefox\Firefox.exe", "http://www.msdn.com", vbNullString, 1

 

____________________________________________

Thats all there is to it!. As you can see with a simple API call you can get the job done. Anyways, have fun!

Jason

14 thoughts on “How to Open a Url or Link in Default WebBrowser – VB 6.0

  1. odod

    Nice post. 😉

    how if i want to change the url when command button on clik? [not open new, but change the url in the address bar]

    Reply
  2. Tim

    Just write in for example a butten:

    process.start(“http://…”)

    you have to have that “HTTP://”, in the action otherwise it won´t work

    Reply
  3. Romke

    Thanks, but using this code with Windows 7 it works only once. After opening the website first time end trying it again it keeps loading an loading but doesnt show the URL.

    Reply
  4. Davis

    Hey I am having issues with the last parameter it seems to have no effect no matter which one I use; it simply opens the web page normally. Also what would be the code to close the browser window after you are finished with it? (I am just using my program to read from the page so the user is not interacting with the site)

    Reply
  5. Anonymous

    Hi,
    I know how to work with shell to invoke a browser and oen URL. I am looking for a way where in I can pass parameters to URL.
    Ex: I can invoke Gmail.com but I want to pass user id and password as parameter so that it will open gmail with my credentials.

    Reply
  6. Jason Post author

    Hi, I am not at all sure how to get the browser to log in for you through sending arguments when the program executes. If I come up with anything i’ll let you know.

    Jason

    Reply

Leave a Reply

Your email address will not be published.