How to Open a Website in Visual Basic


Create a program in vb6 that will allow the user to enter the address of the website in a textbox and open it in the default browser.


We will be using the ShellExecute Windows API (Application Programming Interface). ShellExecute uses the shell to open or print a file or run a program.

Let’s start the tutorial

  1. Kindly open your visual basic 6 program and select Standard EXE.
  2. In this program we need the following controls:
    1. 1 label
    2. 1 textbox
    3. 2 command buttons
  3. The design of the form will be
  4. Next is right click the form and select View Code.
  1. Then paste the code below.
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

Code Explanation:

This is the ShellExecute API that we’re talking about.

  1. Next is we will create a function that will open the site. Paste the code below.
Public Function OpenLocation(url As String, WindowState As Long) As Long
Dim lhWnd As Long
Dim lAns As Long
' Execute the url
lAns = ShellExecute(lhWnd, "open", url, vbNullString, vbNullString, WindowState)
OpenLocation = lAns
End Function
  1. Double click the Open button and paste the code.
Dim i As Long
Dim urls As String
Dim indexpath As String
indexpath = "http://" & text1.Text & ""
urls = Trim(indexpath)
i = OpenLocation(urls, 1)
  1. To close the form, double click the close button and enter the code
Unload me
  1. Save the project and press F5 to run the project.



, ,

Post navigation