|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
下面是一段VBA获取网络时间并更新系统时间的VBA,但是一但断网就会报错,想请各位老师帮下手,修改一下,使其在联网状态下自动获取,断网时则获取系统内部的时间,谢谢!
Dim objXML As Object
Dim strTemp As String
Dim lStart As Long
Dim lEnd As Long
Dim DtWeb As Date
Dim arr
Set objXML = CreateObject("Microsoft.XMLHTTP")
Randomize '初始化随机数,避免IE缓存重复
objXML.Open "Get", "http://www.timeanddate.com/worldclock/city.html?n=33&Refresh=" & Rnd, False
objXML.sEnd ""
strTemp = objXML.responseText
Set objXML = Nothing
lStart = InStr(1, strTemp, "Current Time", vbTextCompare)
lEnd = InStr(lStart, strTemp, "</strong>", vbTextCompare)
strTemp = Mid(strTemp, lStart, lEnd - lStart)
strTemp = Replace(strTemp, "Current Time</th><td><strong id=ct class=big>", "")
arr = Split(strTemp, ",")
网络时间 = CDate(arr(1) & arr(2))
Date = CDate(arr(1))
MsgBox CDate(arr(1))
Range("a1") = VBA.Date
If Date = "" Then
Range("a1") = CurrentDate
End If
红色的那行,就是断网时报错的地方
|
|