VBA – Validate If A URL Exists

When we’re going to work with a file, it is usually a good idea to first test to ensure the file exists. The same is true in the world of web programming.

For instance, I have an article that demonstrates how you can download files from a URL:

and in it I trap the error that the file/resource cannot be accessed/located.  That said, it would be a good idea, to start off, by checking to ensure that the domain URL exists.

Furthermore, such functions can also be useful for validating data entry to ensure that someone didn’t make a typo.
 

The Solution

Option 1 – Microsoft WinHTTP Services

'---------------------------------------------------------------------------------------
' Procedure : WinHTTP_URLExist
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Check and see if a URL exists or not, based on the returned Status Code
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> None required
'             Early Binding -> Microsoft WinHTTP Services, version X.X
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL      : URL to validate
'
' Usage:
' ~~~~~~
' ? WinHTTP_URLExist("https://www.google.ca")
'   Returns -> True
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2012-07-07
' 2         2023-03-23              Updated Error Handling & Header
'---------------------------------------------------------------------------------------
Function WinHTTP_URLExist(ByVal sURL As String) As Boolean
    On Error GoTo Error_Handler
    #Const WinHTTP_EarlyBind = False   'True => Early Binding / False => Late Binding
    #If WinHTTP_EarlyBind = True Then
        Dim oWinHttp          As WinHttp.WinHttpRequest

        Set oWinHttp = New WinHttp.WinHttpRequest
    #Else
        Dim oWinHttp          As Object
        Const WinHttpRequestOption_UserAgentString = 0
        Const WinHttpRequestOption_EnableRedirects = 6

        Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    #End If
    Dim lStatusCode           As Integer

    With oWinHttp
        .Option(WinHttpRequestOption_EnableRedirects) = False
        .Option(WinHttpRequestOption_UserAgentString) = "curl"
        .SetTimeouts 3000, 5000, 3000, 5000
        .Open "HEAD", sURL, False
        .Send
        lStatusCode = .Status
    End With
    WinHTTP_URLExist = (lStatusCode = 200)

Error_Handler_Exit:
    On Error Resume Next
    Set oWinHttp = Nothing
    Exit Function

Error_Handler:
    '-2147012889   The server name or address could not be resolved
    '-2147012894   The operation timed out
    If Err.Number <> -2147012889 And Err.Number <> -2147012894 Then
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Source: WinHTTP_URLExist" & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function

 

Option 2 – Microsoft XML

'---------------------------------------------------------------------------------------
' Procedure : MSXML_URLExist
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Check and see if a URL exists or not, based on the returned Status Code
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Late Binding  -> None required
'             Early Binding -> Microsoft XML, vX.X
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sURL      : URL to validate
'
' Usage:
' ~~~~~~
' ? MSXML_URLExist("https://www.google.ca")
'   Returns -> True
' ? MSXML_URLExist("https://www.devhut.com")
'   Returns -> False
'
' Revision History:
' Rev       Date(yyyy-mm-dd)        Description
' **************************************************************************************
' 1         2012-07-07
' 2         2023-03-23              Updated Error Handling & Header
'---------------------------------------------------------------------------------------
Function MSXML_URLExist(ByVal sURL As String) As Boolean
'https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms760403(v=vs.85)
    On Error GoTo Error_Handler
    #Const MSXML_EarlyBind = False      'True => Early Binding / False => Late Binding
    #If MSXML_EarlyBind = True Then
        Dim oXMLHttp          As MSXML2.ServerXMLHTTP60

        Set oXMLHttp = New MSXML2.ServerXMLHTTP60
    #Else
        Dim oXMLHttp          As Object

        Set oXMLHttp = CreateObject("MSXML2.ServerXMLHTTP")
    #End If
    Dim lStatusCode           As Integer

    With oXMLHttp
        .SetTimeouts 3000, 5000, 3000, 5000
        .Open "HEAD", sURL, False
        .Send
        lStatusCode = .Status
    End With
    MSXML_URLExist = (lStatusCode = 200)

Error_Handler_Exit:
    On Error Resume Next
    Set oXMLHttp = Nothing
    Exit Function

Error_Handler:
    '-2147012889   The server name or address could not be resolved
    '-2147012894   The operation timed out
    If Err.Number <> -2147012889 And Err.Number <> -2147012894 Then
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Source: MSXML_URLExist" & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function

 

HTTP Response Status Codes

Do note that in both instances I am validating the HTTP response status codes and only accepting a value of 200 as indicating that the URL is valid. This is a very strict definition of ‘valid’. So you may wish to review the complete list of HTTP response status codes using the link below and modify the validation to include other code, perhaps 200 – 299.

14 responses on “VBA – Validate If A URL Exists

  1. Dongchu Sun

    When I run URLExists (I copied your code) always get eror message:
    compile Error:
    User-defined type not defined.
    OK Help
    I think I missing something. Maybe I need declare some .dll file? But it working fine before.

    1. Daniel Pineault Post author

      Not sure which function you are referring to, but that error is most probably occurring because you have the early binding conditional compilation equal to True, thus in early binding requiring you to set the specified reference as indicated in the function header.

      1. Dongchu Sun

        Thank you very much for your apply. I saw it request refference Late and early binding. How can I do that? Please advise me. Thank you.
        Req’d Refs: Late Binding -> None required
        ‘ Early Binding -> Microsoft WinHTTP Services, version X.X

  2. Dongchu Sun

    Thank you very very much. I know how to do it now. I am very glad we have an web site here and have coding expert answer beginner like me’s questions. Thanks again.

  3. Dongchu Sun

    I am sorry that my coding is still not working. In the Function URLExists, rc = .StatusText is empty, So, the Function eturn false. The file 1.Txt is exists on the web. Where did I do wrong? Thank you for your any comments.

    Private Sub Command1_Click()
    If URLExists(“https://www.food-coral.com/store/orders/1.txt”) Then
    MsgBox “File is Exists”
    Else
    MsgBox “File is not exist.”
    End If
    If URLExists(“https://www.food-coral.com/store/orders”) Then
    MsgBox “Folder is Exists”
    Else
    MsgBox “Folder is not exist.”
    End If

    End Sub

    Private Function URLExists(URL As String) As Boolean
    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant
    URLExists = False
    On Error Resume Next ‘GoTo EndNow
    Set Request = CreateObject(“WinHttp.WinHttpRequest.5.1”)

    With Request
    .Open “GET”, URL, False
    .Send
    rc = .StatusText
    End With
    Set Request = Nothing
    ‘If rc = “OK” Then URLExists = True
    URLExists = rc = “OK”
    Exit Function
    EndNow:
    End Function

  4. Dongchu Sun

    Hi, Daniel, Thank you for your response. The code is still not working on my side. I only add Microsoft WinHTTP Service version 5.1 and no other binding. Which binding I should add?

    I tied your code at the biginning and I wrote the commends there and message:
    compile Error:
    User-defined type not defined.
    OK Help

  5. Dongchu Sun

    Hi, Except Microsoft WinHttp Service version 5.1 need to be binding. In order to let my code for URLExists is working, anything else need to binding? I am waiting for the answer. My code need to work ASAP. Thank you.

    Perhaps, remove the error handler and see what happens. Does an error get raised.

    You really should post this in an Access or VBA forum to get further assistance.

    1. Daniel Pineault Post author

      If you’re talking about the code you posted earlier, you’re using CreateObject, thus Late Binding, hence no reference libraries are required.

  6. Dongchu Sun

    Hi, Daniel, you are the expert, what is your advise? I just need the function URLExists and then ReadURLFile. The first Code is working at your compter not mine. But I really need it working. What should I do?

  7. Dongchu Sun

    Hi, Daniel,

    Thank you for your time. Thank you for your advis.
    Do you have any solution for my situation? I just need:
    1. I can find any new .txt on my website
    2. Read the .txt file

    That is ALL.

  8. atkins

    Hello

    Both function doesnt work on 64 bit office and returns false, while they work fine on 32 bit. Do you have any suggestion?

    Thanks