【问题标题】:VBA download file to a string variableVBA 将文件下载到字符串变量
【发布时间】:2017-04-04 08:31:13
【问题描述】:

如何将文件从 URL 下载到字符串变量而不将其保存到本地驱动器?与 C# 中的此方法类似,我希望在 VBA 中使用它:Download file from URL to a string

这里有一些关于将文件下载到磁盘空间的内容:How do i download a file using VBA (Without internet explorer) 虽然这可能是一些提示,但我想避免将文件转换为字符串的这个中间步骤。

【问题讨论】:

    标签: string vba excel curl


    【解决方案1】:

    使用WinHttpRequest类可以实现文件,.Open方法需要2个参数:请求方法“GET”/“POST”,URL字符串。
    第三个参数是可选的,布尔型,异步模式默认为True:

    Set httpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "http://urlo_of_the_file_you_want"
    httpRequest.Open "GET", URL, False
    httpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
    httpRequest.WaitForResponse
    Debug.Print httpRequest.Status
    Debug.Print httpRequest.ResponseText
    mystring = httpRequest.ResponseText
    

    然后你就得到了 var name mystring中的文件

    【讨论】:

    • 这很好用,只是你似乎忘记了:codehttpRequest.Sendcode 在 WaitForResponse 之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2022-09-29
    • 2015-01-16
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多