Check URL

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code checks if an URL exists:

Code:
Function URLExists(url As String) As Boolean
    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant
    
    On Error 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
    
    Exit Function
EndNow:
End Function

However it doesn't seem to work if I put in an URL such as:

Code:
"bbc.co.uk"

Instead I have to put:

Code:
"https://www.bbc.co.uk"

How can I adapt the code so it will accept both urls?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
The protocol is part of the URL so without it you are guessing. If the URL does not begin with HTTP you can try HTTPS then if that doesn't work try HTTP, and if that doesn't work return FALSE.

Here is version that solves it recursively. Compiled but not tested.
Rich (BB code):
Function URLExists(url As String) As Boolean

    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant
    
    On Error GoTo EndNow
    Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    If UCase(Left(url, 4)) <> "HTTP" Then
      URLExists = URLExists("http://" & url)
      If Not URLExists Then
         URLExists = URLExists("https://" & url)
      End If
      
    Else
    
      With Request
        .Open "GET", url, False
        .Send
        rc = .StatusText
      End With
      Set Request = Nothing
      If rc = "OK" Then URLExists = True
    
    End If
    
    Exit Function
    
EndNow:

End Function
 
Upvote 0
Solution
The protocol is part of the URL so without it you are guessing. If the URL does not begin with HTTP you can try HTTPS then if that doesn't work try HTTP, and if that doesn't work return FALSE.

Here is version that solves it recursively. Compiled but not tested.
Rich (BB code):
Function URLExists(url As String) As Boolean

    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant
   
    On Error GoTo EndNow
    Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    If UCase(Left(url, 4)) <> "HTTP" Then
      URLExists = URLExists("http://" & url)
      If Not URLExists Then
         URLExists = URLExists("https://" & url)
      End If
      
    Else
   
      With Request
        .Open "GET", url, False
        .Send
        rc = .StatusText
      End With
      Set Request = Nothing
      If rc = "OK" Then URLExists = True
   
    End If
   
    Exit Function
   
EndNow:

End Function
Thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top