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:
- The program would open a worksheet.
- It would use OLE Automation to read the first URL and see whether it's functioning.
- Write data back to the worksheet, indicating the result for the URL.
- Repeat the preceding steps for all the URLs in the list.
- 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.
Const FILE = "C:\hrd_links.xls"
Here is the complete code of the application:
Set objExcel = CreateObject("Excel.Application")
Inet1.Protocol = icHTTP
Public Sub Check_Links()
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
On Error Resume Next
var_row = STARTROW
var_timeout = 0
var_file_not_found = 0
var_OK = 0
var_server_not_found = 0
frmmain.WindowState = 1
var_URL = objExcel.Cells(var_row, URL_COL)
If var_URL = "" Then Exit Do
Text1.Text = Inet1.OpenURL(var_URL)
If Len(Text1.Text) > 50 Then
var_buffer = Left(Text1.Text, 50)
var_buffer = Text1.Text
If Err = 35761 Then
var_msg = "Timed Out"
var_timeout = var_timeout + 1
ElseIf Text1.Text = "" Then
var_msg = "Server not found"
var_server_not_found = var_server_not_found + 1
ElseIf InStr(1, var_buffer, "404") Then
var_msg = "File not found"
var_file_not_found = var_file_not_found + 1
var_msg = "OK"
var_OK = var_OK + 1
objExcel.Cells(var_row, STATUS) = var_msg
var_row = var_row + 1
frmmain.Caption = var_OK + var_file_not_found + _
var_server_not_found + var_timeout
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
frmmain.WindowState = 0
Set objExcel = Nothing
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
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.
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 email@example.com