【转载】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选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access自定义函数命名为中文名...(09.20)
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)