Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Explicit
Sub CheckHyperlinks()
Dim oWB As Workbook
Set oWB = ActiveWorkbook
Dim oFS As FileSystemObject
Set oFS = New FileSystemObject
Dim oSh As Worksheet
Dim oHyp As Hyperlink
Dim sAddress As String
For Each oSh In oWB.Worksheets
For Each oHyp In oSh.Hyperlinks
'CHECK WHETHER LINK IS BROKEN HERE
Next oHyp
Next oSh
Set oHyp = Nothing
Set oSh = Nothing
Set oWB = Nothing
Set oFS = Nothing
End Sub
sAddress = oHyp.Address
If sAddress Like "http:*" Then
'Add some code to check URL here
Else
If Not oFS.FileExists(sAddress) Then
MsgBox "Broken link: " & sAddress
'Deal with broken link
'e.g. delete link or write info somewhere
End If
End If
If InStr(objIE.Document.Body.innerText, "The page could not be displayed") > 0 Then[green]
'The link is not valid, flag the cell[/green]
rowCurrent.Interior.Color = vbRed
End If
[blue]Public Sub[/blue] CheckAllHyperlinks()
Dim objIE As Object
Dim rngHyperlinks As Range
Dim rowCurrent As Range
[green]
'Hard coded the column with the hyperlinks, used A1
'from the active sheet for this example[/green]
Set rngHyperlinks = ActiveSheet.Range("A1").CurrentRegion
[green]
'Spawn an IE window and make it visible[/green]
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
[green]
'Iterate through all the rows in the current region[/green]
For Each rowCurrent In rngHyperlinks.Rows
objIE.Navigate rowCurrent.Hyperlinks(1).Address
While objIE.Busy[green]
'Wait until the page loads[/green]
Wend[green]
'***
'Check if the page returned an error here, possible ways:
' Debug.Print objIE.Document.Body.innerText
' Debug.Print objIE.Document.Body.innerHTML
'***
'Pause execution so you can see what happened[/green][blue]
Stop[/blue]
Next rowCurrent
[green]
'Close IE and release the object[/green]
objIE.Quit
Set objIE = Nothing
[green]
'Clean up[/green]
Set rowCurrent = Nothing
Set rngHyperlinks = Nothing
[blue]End Sub[/blue]
'*****************requires link to scripting runtime library************************************
'********************** by Matt Whetton, 01 September 2005 *************************************
Option Explicit
'api declaration
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Sub TestAllHyperLinks(WS As Worksheet)
Dim hp As Hyperlink, newWs As Worksheet, wkbk As Workbook, counter As Integer
Dim HFlag As Boolean
Set wkbk = WS.Parent
Set newWs = wkbk.Sheets.Add
counter = 1
'this allows the user to specify if they want to have the cells highlighting that have
'broken links
If MsgBox("Do you want broken links highlighting?", vbYesNo) = vbYes Then HFlag = True
For Each hp In WS.Hyperlinks
If DoesURLExist(hp.Address) Then
'yes it does, no action required
Else
'no it doesnt
newWs.Cells(counter, 1) = hp.Name
newWs.Cells(counter, 2) = hp.Address
newWs.Cells(counter, 3) = hp.Range.Address
If HFlag = True Then WS.Range(hp.Range.Address).Interior.ColorIndex = 3
counter = counter + 1
End If
Next
End Sub
Private Function DoesURLExist(URL As String) As Boolean
'function returns false also if url doesnt exist, but also deals with deleting the
'temporary file created by DownloadFile function
Dim fs As FileSystemObject, Temp101 As String
Set fs = New FileSystemObject
Temp101 = Environ("TEMP")
'fs.CreateFolder (Temp101)
If DownloadFile(URL, Temp101 & "\xxxx") = True Then
DoesURLExist = True
Kill Temp101 & "\xxxx"
Else
DoesURLExist = False
End If
'fs.DeleteFolder ("C:\temp101")
End Function
Private Function DownloadFile(URL As String, LocalFilename As String) As Boolean
'function uses api to retrieve file and return false if doesnt complete
Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
End Function