Custom function for checking validity of a hyperlink

mbt2103

New Member
Joined
Aug 4, 2014
Messages
35
As the subject says, I'm trying to create a custom function in VBA that will check the validity of a hyperlink. The purpose of this is to have a personnel tracking sheet with hyperlinks to supporting documentation (such as a scan of the employee's diploma) which, when the supporting documentation doesn't exist, display something like "NOT AVAILABLE" in the cell.

All of this is hosted on an internal sharepoint server that is password protected and heavily encrypted, because I work at a healthcare facility. This means that the links are web addresses, not file structure links.

I almost have it working. Here's what I have so far (this requires a reference to Microsoft WinHTTP Services, version 5.1):

Code:
Dim oURL As New WinHttpRequestDim linkStatus As Integer


On Error GoTo errorhandler


With oURL
    .Option(WinHttpRequestOption_EnableRedirects) = False
    .Open "POST", link, False
    .Send ("")
    testhyperlink = .ResponseText
    Exit Function
End With


errorhandler:
    testhyperlink = "Error"


End Function

This function works wonderfully for websites that do not require a login (also, I have to mention that it is mostly an adaptation of a macro built by someone on this forum several years back, which I found during my initial search for answers).

For websites that require a login, it ALWAYS returns: "401 UNAUTHORIZED", whether or not the linked item exists.

When I use the HYPERLINK function and click on the link from excel, however, it never asks me for login credentials (I have to login to access the document in the first place), instead, it immediately gives me one of two excel error windows. If the file exists, it says: "Opening [this file] Some files can contain viruses ... Would you like to open this file?" but if the file doesn't exist, it says: "Unable to open [this file]. The internet site reports that the item you requested could not be found. (HTTP/1.0 404)"

If I could capture those windows, I could make it work! But I don't know how ...

Any thoughts?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Solved.

Add a reference to Microsoft XML, v6.0, then use the following code:
Code:
Function testhyperlink(link As String) As Boolean
Dim oURL As New WinHttpRequest
Dim linkStatus As Integer


On Error GoTo errorhandler


Dim linkRequest As Object
Set linkRequest = New MSXML2.XMLHTTP60
With linkRequest
    .Open "GET", link, False
    .SetRequestHeader "Cache-Control", "no-cache"
    .SetRequestHeader "Pragma", "no-cache"
    .SetRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .Send
End With
testhyperlink = True
If linkRequest.Status = 404 Then testhyperlink = False
Exit Function


errorhandler:
    testhyperlink = False


End Function
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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