Click here to Skip to main content
15,884,673 members
Articles / Programming Languages / VBScript
Article

Create a program that checks all the links in your web page whether they are active or not.

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
12 Nov 20013 min read 123.5K   2.8K   23   13
This article will show you how to test your web page for active links. You will learn how to use the Internet Transfer Control. This is a real world example of Inet usage.

Sample Image - linkchecker.jpg

Introduction

In this article, I will show you how to create a program that checks whether the links in your web application are active or not. We will use Internet Transfer Control to accomplish this task. This is a real world example where you will see the real use of Internet Transfer Control.

About the application

Internet Transfer Control is a very handy control. The program will check each link in the web page to see if it's functioning or not. Suppose, I add web page addresses of different people on my web page. Now, the web pages usually move to different locations or people simply put their sites off the Internet, and all of a sudden my links are dead. If I have more than 100 links on my page, then to check all those pages manually will be a hectic task and we can't check the URLs on a regular basis. So, we need to automate this process. An easy way is to keep a links database either in Access or in Excel and then check all the links in the database whether they are functioning. You can populate your web page with the live links from your database.

What this program does?

The program we are going to create will perform the following tasks:

  1. The program would open a worksheet.
  2. It would use OLE Automation to read the first URL and see whether it's functioning.
  3. Write data back to the worksheet, indicating the result for the URL.
  4. Repeat the preceding steps for all the URLs in the list.
  5. Save and close the worksheet.

What is OLE?

OLE stands for Object Linking and Embedding. It is a technology for transferring and sharing information among applications. Different applications expose their objects that are related to the kind of data the application works with. Automation client is an application that expose objects belonging to another application. In our case, our VB program will act as an Automation client. An Automation server is an application that exposes programmable objects to other applications. In our case, Excel will act as an Automation server. Excel exposes Automation objects. These objects have properties and methods as their external interface. Properties are named attributes of the Automation object. Methods are functions that work on an Automation object.

More about the application

Let's discuss the layout of the Excel worksheet where the data will be stored. When the program has completed the task, each record in the Excel worksheet will contain an entry specifying the current status of the URL. The application will run minimized in the background while your other applications work normally. The number of links already checked is displayed in the program's title bar and therefore also displayed on the taskbar icon when the program is minimized. This enables the user to keep track of the progress. When the URLs have been checked, the program closes the worksheet and displays a summary report.

URL will be saved in column C, the third cell from the left, and the information as to whether the link is okay is kept in column E. Data begins in row 4.

I have included detailed comments in the code so that you can see how it works. You will have to edit the code before running it. Change the path of the Excel file in the code.

VB
'Save the filename in a variable

Const FILE = "C:\hrd_links.xls" 
'(This variable should contain the exact path of the file)

Here is the complete code of the application:

VB
'Create the Excel object

Set objExcel = CreateObject("Excel.Application")

'Open the worksheet
objExcel.Workbooks.Open MYFILE

'Set the Transfer Protocol
Inet1.Protocol = icHTTP

'This is the main function!!
Public Sub Check_Links()

'This function will be called to check all the links in the
'worksheet.

'Declare variables
Dim var_row As Integer
Dim var_URL As String
Dim var_buffer As String
Dim var_msg As String
Dim var_file_not_found As Integer
Dim var_server_not_found As Integer
Dim var_timeout As Integer
Dim var_OK As Integer

'Catch the time-out errors

On Error Resume Next

'Set the row variable to the cell where the data starts
var_row = STARTROW

'Initialize the variables

var_timeout = 0
var_file_not_found = 0
var_OK = 0
var_server_not_found = 0


'Minimize the form
frmmain.WindowState = 1


'Loop through all the URLs
Do
    'Get the URL
    var_URL = objExcel.Cells(var_row, URL_COL)

    'Check whether the first cell is empty
    If var_URL = "" Then Exit Do

    'Open the URL
    Text1.Text = Inet1.OpenURL(var_URL)

    'Avoid tying up the system
    DoEvents

    'Errors messages are found in the  first 50 characters
    'returned by the openurl method

    If Len(Text1.Text) > 50 Then

        var_buffer = Left(Text1.Text, 50)

    Else

        var_buffer = Text1.Text

    End If

    'Catch a time-out error

    If Err = 35761 Then
         var_msg = "Timed Out"
         var_timeout = var_timeout + 1
    Err.Clear

    'If nothing is returned, it means that the server was
        'not found

    ElseIf Text1.Text = "" Then

        var_msg = "Server not found"
        var_server_not_found = var_server_not_found + 1

        'If error 404 is returned from the URL, it means the
       'server was found but he file was not found

    ElseIf InStr(1, var_buffer, "404") Then

        var_msg = "File not found"
       var_file_not_found = var_file_not_found + 1

    'else, the link is OK

    Else

        var_msg = "OK"
        var_OK = var_OK + 1

    End If

    'Save the result back to the worksheet

    objExcel.Cells(var_row, STATUS) = var_msg

    'Move to the next row

    var_row = var_row + 1

    'Display the current status.

    frmmain.Caption = var_OK + var_file_not_found + _ 
                            var_server_not_found + var_timeout

    'Display the results on the form

    Label1.Caption = "OK: " & var_OK
    Label2.Caption = "File not found: " & var_file_not_found
    Label3.Caption = "Server not found: " & var_server_not_found
    Label4.Caption = "Timed out: " & var_timeout

Loop While True

'If all the links have been checked, restore the form

frmmain.WindowState = 0

'Close the Worksheet

objExcel.Workbooks.Close

'Remove the object from the memory
Set objExcel = Nothing

'Display the results

var_buffer = "OK: " & var_OK & vbCrLf
var_buffer = var_buffer & "Server not found: " & _ 
                       var_server_not_found & vbCrLf
var_buffer = var_buffer & "File not found: " & _ 
                       var_file_not_found & vbCrLf
var_buffer = var_buffer & "Timed out: " & var_timeout

MsgBox var_buffer

Open the Excel workbook and add the links you want the program to check. Close the workbook and run the program. Make sure you are connected to the Internet. That's it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
S.S. Ahmed is a senior software engineer and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic web sites. He has been creating customized SharePoint solutions for the last five years. Ahmed has worked in Visual Basic, Visual C, Java, ASP, .NET, SharePoint, InfoPath, BizTalk, etc. Ahmed enjoys travelling and has been to many parts of the world. Ahmed distributes most of his work freely to many internet sites. Ahmed also participates in different newsgroups and tries to help people out with their problems. Ahmed can be reached at share.point@yahoo.com

PEACE

S.S. Ahmed
Web: www.walisystems.com
Blog:www.sharepointblogs.com/ssa

Comments and Discussions

 
GeneralReg: VB Script Pin
maddy9910-Jun-08 2:32
maddy9910-Jun-08 2:32 
Generalhai Pin
sara_par29-Nov-04 3:34
sara_par29-Nov-04 3:34 
GeneralNot bad Pin
Michael P Butler13-Nov-01 4:22
Michael P Butler13-Nov-01 4:22 
GeneralRe: Not bad Pin
S.S. Ahmed13-Nov-01 21:44
S.S. Ahmed13-Nov-01 21:44 
GeneralThe Script Not working Please help Pin
Venkat Kandikonda29-May-12 1:20
Venkat Kandikonda29-May-12 1:20 
GeneralIts VB Pin
13-Nov-01 0:50
suss13-Nov-01 0:50 
GeneralRe: Its VB Pin
Pete Bassett13-Nov-01 0:53
Pete Bassett13-Nov-01 0:53 
GeneralRe: Its VB Pin
13-Nov-01 2:13
suss13-Nov-01 2:13 
GeneralRe: Its VB Pin
S.S. Ahmed13-Nov-01 4:09
S.S. Ahmed13-Nov-01 4:09 
GeneralRe: Its VB Pin
Michael P Butler13-Nov-01 4:17
Michael P Butler13-Nov-01 4:17 
GeneralRe: Its VB Pin
13-Nov-01 5:20
suss13-Nov-01 5:20 
GeneralRe: Its VB Pin
13-Nov-01 6:48
suss13-Nov-01 6:48 
GeneralRe: Its VB Pin
David Crow10-Oct-08 3:32
David Crow10-Oct-08 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.