Вы можете повысить эффективность, создав объект xhr в подпрограмме и перейдя к функции, а затем посмотрите только на заголовок ответа link
, чтобы различить
Option Explicit
Public Sub Test()
Dim urls(), i As Long, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal url As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", url, False
.send
CheckValidURL = Not .getResponseHeader("link") = vbNullString
End With
End Function
Альтернативы:
В функциональном тесте на наличие идентификатора, который присутствует только в действительных ссылках, или строки (в том виде, как вы это сделали)
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP"): Set html = New HTMLDocument
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr, html)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object, ByVal html As HTMLDocument) As Boolean
With xhr
.Open "GET", sURL, False
.send
html.body.innerHTML = .responseText
End With
CheckValidURL = html.querySelectorAll("#wistia_video").Length > 0
End Function
Также используя Instr работает
Option Explicit
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", sURL, False
.send
CheckValidURL = InStr(.responseText, "html") > 0
End With
End Function
Переписать ваши:
Option Explicit
Public Sub Test()
Dim urls(), i As Long, html As HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
urls = Array("https://fast.wistia.net/embed/iframe/vud7ff4i6wyh", "https://fast.wistia.net/embed/iframe/vud7ff4i6w")
For i = LBound(urls) To UBound(urls)
MsgBox CheckValidURL(urls(i), xhr)
Next
End Sub
Public Function CheckValidURL(ByVal sURL As String, ByVal xhr As Object) As Boolean
With xhr
.Open "GET", sURL, False
.send
CheckValidURL = UBound(Split(.responseText, "<script", , vbTextCompare)) > 0
End With
End Function