Excel VBA发送到MSXML2.XMLHTTP不起作用

时间:2017-09-15 21:19:36

标签: vba excel-vba excel

我在excel VBA中有以下代码,直到昨天才运行良好。 现在它在尝试检查URL时失败了..但URL确实存在!

它失败了: oXHTTP.send (它变为hasError)

Function HttpExists(sURL As String) As Boolean
Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")

If Not UCase(sURL) Like "HTTP:*" Then
    sURL = "http://" & sURL
End If

On Error GoTo haveError
oXHTTP.Open "HEAD", sURL, False
oXHTTP.send
HttpExists = IIf(oXHTTP.status = 200, True, False)
Exit Function

haveError:
    HttpExists = False
End Function

sURL如下,确实存在

http://www.google.com/finance/historical?q=COF&startdate=Jan+1%2C+2017&enddate=Dec+31%2C+2017&num=30&ei=WLQtWaAfiMOxAbeYutgE&output=csv

可能是什么问题?

由于

1 个答案:

答案 0 :(得分:2)

考虑在错误处理块中添加一条消息,该消息提供了一个更具信息性的问题,可以使用MSXML2.ServerXMLHTTP代替MSXML2.XMLHTTP来解决。

查看here的SO帖子。

Function HttpExists(sURL As String) As Boolean

    Dim oXHTTP As Object
    Set oXHTTP = CreateObject("MSXML2.ServerXMLHTTP")

    If Not UCase(sURL) Like "HTTP:*" Then
        sURL = "http://" & sURL
    End If

    On Error GoTo haveError
    oXHTTP.Open "HEAD", sURL, False
    oXHTTP.send
    HttpExists = IIf(oXHTTP.Status = 200, True, False)
    Exit Function

haveError:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
    HttpExists = False
End Function


Sub RunFct()
    Dim output As Boolean

    output = HttpExists("http://www.google.com/finance/historical?q=COF&startdate=Jan+1%2C+2017&enddate=Dec+31%2C+2017&num=30&ei=WLQtWaAfiMOxAbeYutgE&output=csv")
    Debug.Print output
End Sub

立即窗口输出

TRUE