Checking if File Exists using HTTP

Microsoft ships a redistributable msinet.ocx with a number of products including Visual Basic 5.0, 6.0, Office 2000 Developer, Visual FoxPro 6.0, Visual C++ 5.0, 6.0. This file hosts Microsoft Internet Transfer Control which implements a number of protocols including HTTP and FTP. You need to know the commands that initiate the operations.

The code snippet given below shows how to use Microsoft Internet Transfer Control to check if a remote file exists. To use the code, make sure that you add a reference to Microsoft Internet Transfer Control. To add a reference to it

  1. Select Tools | References....
  2. Select Microsoft Internet Transfer Control from the available references.
    1. If it is not available, click the Browse... button. You will be presented with the Add Reference dialog box.
    2. Locate and select MSINET.OCX. This file is usually present in the System folder.
    3. Click Open to dismiss the Add Reference dialog box.
  3. Click OK.

Function DoesHTTPFileExist(ByVal URL As String) As Boolean
    Dim HTTP As Inet
    Dim S As String
    Dim Exists As Boolean

    Set HTTP = New Inet
    With HTTP
        .Protocol = icHTTP
        .URL = URL
        .Execute
        Do While .StillExecuting
            DoEvents
        Loop
        S = UCase(.GetHeader())
        Exists = (InStr(1, S, "200 OK") > 0)
    End With
    Set HTTP = Nothing
    DoesHTTPFileExist = Exists
End Function

Contact OfficeOne on email at officeone@officeoneonline.com. Copyright © 2001-2023 OfficeOne. All rights reserved.