Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

【转载】VBA–验证是否存在URL

时 间:2023-03-28 10:19:00
作 者:金宇   ID:43  城市:江阴
摘 要:VBA–验证是否存在URL。
正 文:

      当我们要处理一个文件时,通常最好先进行测试以确保文件存在,如果我们需要从某个网站下载文件,那么需要先验证这个网址URL是否存在,那么可以通过下列的函数来进行校验。

      可以将下列的代码放到新建的模块中,然后可以在窗体的事件中进行调用,例如: WinHTTP_URLExist("http://www.accessoft.com")

Function WinHTTP_URLExist(ByVal sURL As String) As Boolean
    On Error GoTo Error_Handler
    #Const WinHTTP_EarlyBind = False   'True => Early Binding / False => Late Binding
    #If WinHTTP_EarlyBind = True Then
        Dim oWinHttp          As WinHttp.WinHttpRequest

        Set oWinHttp = New WinHttp.WinHttpRequest
    #Else
        Dim oWinHttp          As Object
        Const WinHttpRequestOption_UserAgentString = 0
        Const WinHttpRequestOption_EnableRedirects = 6

        Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
    #End If
    Dim lStatusCode           As Integer

    With oWinHttp
        .Option(WinHttpRequestOption_EnableRedirects) = False
        .Option(WinHttpRequestOption_UserAgentString) = "curl"
        .SetTimeouts 3000, 5000, 3000, 5000
        .Open "HEAD", sURL, False
        .Send
        lStatusCode = .Status
    End With
    WinHTTP_URLExist = (lStatusCode = 200)

Error_Handler_Exit:
    On Error Resume Next
    Set oWinHttp = Nothing
    Exit Function

Error_Handler:
    '-2147012889   The server name or address could not be resolved
    '-2147012894   The operation timed out
    If Err.Number <> -2147012889 And Err.Number <> -2147012894 Then
        MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
               "Error Source: WinHTTP_URLExist" & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Description: " & Err.Description & _
               Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
               , vbOKOnly + vbCritical, "An Error has Occurred!"
    End If
    Resume Error_Handler_Exit
End Function


Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助