【发布时间】:2020-07-05 15:52:07
【问题描述】:
我正在尝试使用 VBA-Web 库 (https://github.com/VBA-tools/VBA-Web) 下载图像并将其保存到我的计算机。
此代码运行良好,但我想检查其完成工作的正确方法。 VBA 不是我的主要经验。
Sub Run()
Dim client As New WebClient
With client
.BaseUrl = "http://chart.apis.google.com/chart?cht=qr&chs=160x160&chld=L|0&chl=hello"
.EnableAutoProxy = True
End With
Dim request As New WebRequest
With request
.Method = WebMethod.HttpGet
.AddHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
End With
Dim Response As WebResponse
Set Response = client.Execute(request)
ProcessResponse Response
End Sub
Sub ProcessResponse(Response As WebResponse)
Dim oStream As Object
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write Response.Body
oStream.SaveToFile Environ("USERPROFILE") & "\Desktop\image_test.png", 2
oStream.Close
End Sub
我使用过各种其他方法进行下载,包括 XMLHTTP 和 URLDownloadToFile,但由于网络问题,我需要 VBA-Web 提供的代理处理...
【问题讨论】:
-
嗨,如果它有效,但您正在寻找批评,请考虑code review site。 SO适用于特定的编程问题。从表面上看,代码看起来很好。
-
啊,谢谢。我很抱歉。