How to Open a Url or Link in Default Browser – Visual Basic 6.0

  • by
open link vb

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.

open link vb

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 that’s 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 parameters is not required for this to work so I just set them to 0 or vb null string.

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", "", 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 web browser 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 web browser and how to get it to go to a specific url.
ShellExecute 0, "open", "C:\Program Files\Mozilla Firefox\Firefox.exe", "", vbNullString, 1

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

Leave a Reply

Your email address will not be published. Required fields are marked *