【转载】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群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access对子窗体数据进行批...(10.30)
- 最精简的组合框行来源数据快速输...(10.25)
- Access仿平台的多值选择器...(10.24)
- 【Access日期区间段查询】...(10.22)
- 【Access源码示例】VBA...(10.12)
- Access累乘示例,Acce...(10.09)
- 数值8.88,把整数8去掉,转...(10.08)
- 【Access自定义函数】一个...(09.30)
- 【Access选项卡示例】Ac...(09.09)
学习心得
最新文章
- Access判断多条明细的配方或订...(11.30)
- 如何让后台数据库在局域网共享时,且...(11.29)
- 【Access月初月末日期设置方法...(11.29)
- 【Access IIF函数嵌套示例...(11.26)
- Access快速开发平台--使用组...(11.25)
- Access快速开发平台--对上传...(11.22)
- Access快速开发平台企业版--...(11.18)
- 不会用多表联合查询,多表查询没结果...(11.16)
- 【案例分享】主键字段值含有不间断空...(11.16)
- Access快速开发平台--后台D...(11.14)