【问题标题】:VBA get real time from internet get error: The download of the specified resource has failedVBA get real time from internet get error: The download of the specified resource has failed
【发布时间】:2021-08-02 14:35:07
【问题描述】:

我想从互联网实时获取 Excel 文件,在我的电脑上它不起作用,但在另一台电脑上它可以工作。

我电脑上的错误是:

“指定资源下载失败。”

Function GetUCTTimeDate() As Date
Dim UTCDateTime As String
Dim arrDT() As String
Dim http As Object
Dim UTCDate As String
Dim UTCTime As String

Const NetTime As String = "https://www.time.gov/"

On Error Resume Next
Set http = CreateObject("Microsoft.XMLHTTP")
On Error GoTo 0

http.Open "GET", NetTime & Now(), False, "", ""
http.send

UTCDateTime = http.getResponseHeader("Date")
UTCDate = Mid(UTCDateTime, InStr(UTCDateTime, ",") + 2)
UTCDate = Left(UTCDate, InStrRev(UTCDate, " ") - 1)
UTCTime = Mid(UTCDate, InStrRev(UTCDate, " ") + 1)
UTCDate = Left(UTCDate, InStrRev(UTCDate, " ") - 1)
GetUCTTimeDate = DateValue(UTCDate) + TimeValue(UTCTime)
End Function

【问题讨论】:

  • 代码在我的工作站上正常工作,没有错误。我唯一的猜测是您偶尔会遇到将您的请求路由到指定 URL 的网络问题。
  • 你认为代码依赖于IE版本吗?
  • 我的猜测是否定的,但请记住这是合理的想法。我建议继续使用其他网站和/或代码进行测试,看看问题是否仍然存在或改变(或消失)。在网络上搜索vba rest api 可以引导您找到一些示例to test

标签: vba api


【解决方案1】:

我之前也遇到过同样的问题。它在我的电脑上运行良好,但在另一台电脑上出错。

为了解决这个问题,将函数“As String”而不是“As Date”调暗

Public Function GetUCTTimeDate() As String  

并从GetUCTTimeDate = DateValue(UTCDate)中删除“DateValue”

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。它发生在我将计算机的格式日期从 mm/dd/yyyy 更改为 dd/mm/yyyy 时。我试图解决它,我发现的解决方案是将日期传递给工作表,然后再次接受。

    Function GetUCTTimeDate() As Date
        Dim UTCDateTime As String
        Dim arrDT() As String
        Dim http As Object
        Dim UTCDate As String
        Dim UTCTime As String
    
        Const NetTime As String = "https://www.time.gov/"
    
        On Error Resume Next
        Set http = CreateObject("Microsoft.XMLHTTP")
        On Error GoTo 0
    
        http.Open "GET", NetTime & Now(), False, "", ""
        http.send
    
        UTCDateTime = http.getResponseHeader("Date")
        UTCDate = Mid(UTCDateTime, InStr(UTCDateTime, ",") + 2)
        UTCDate = Left(UTCDate, InStrRev(UTCDate, " ") - 1)
        Worksheets("Hoja1").Range("C2") = UTCDate   'Here I passed to the sheet
        UTCTime = Mid(UTCDate, InStrRev(UTCDate, " ") + 1)
        UTCDate = Left(UTCDate, InStrRev(UTCDate, " ") - 1)
        GetUCTTimeDate = Worksheets("Hoja1").Range("C2") 'Getting again the date
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 2022-12-27
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2019-02-18
      • 2022-11-20
      • 2011-12-30
      相关资源
      最近更新 更多