【发布时间】:2019-10-02 14:04:33
【问题描述】:
我在 excel 上有一个很长的超链接列表,我想创建一个代码来检查这些链接是否会驱动到错误页面。
我改编了这篇帖子Sort dead hyperlinks in Excel with VBA?的代码
但是每次运行都会报错
“403 - 禁止”
出现,无论链接是否有效。
我希望代码执行的是在下一个单元格中写入是否会导致 404 页面。 我想问题是缺少授权 excel 跟随超链接的额外行,但我想不出如何解决这个问题。
这是我正在使用的代码:
Sub CheckHyperlinks()
Dim oColumn As Range
Dim oCell As Range
For Each oCell In Selection
If oCell.Hyperlinks.Count > 0 Then
Dim oHyperlink As Hyperlink
Set oHyperlink = oCell.Hyperlinks(1) ' I assume only 1 hyperlink per cell
Dim strResult As String
strResult = GetResult(oHyperlink.Address)
oCell.Offset(0, 1).Value = strResult
End If
Next oCell
End Sub
Private Function GetResult(ByVal strUrl As String) As String
On Error GoTo ErrorHandler
Dim oHttp As New MSXML2.XMLHTTP60
oHttp.Open "HEAD", strUrl, False
oHttp.send
GetResult = oHttp.Status & " " & oHttp.statusText
Exit Function
ErrorHandler:
GetResult = "Error: " & Err.Description
End Function
【问题讨论】:
标签: excel vba hyperlink reference http-status-code-403