我使用以下代码从Excel VBA上的Web下载.xlsx文件。
Sub Download()
Const MYURL = "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx"
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", MYURL, False
objHTTP.Send
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write objHTTP.ResponseBody
oStream.SaveToFile ("C:\wamp\file.xlsx")
oStream.Close
End Sub
代码成功下载文件并将其保存在目录中。但是我希望我可以在Excel上打开xlsx文件,而不是保存它。有可能吗?
答案 0 :(得分:1)
Set wb = WorkBooks.open( _
"https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx")
答案 1 :(得分:0)
因此经过一些研究而不是尝试直接从内存中打开文件,我只是将其保存在用户的默认临时路径上然后打开它。这是代码:
Sub Download()
Const MYURL = "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx"
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", MYURL, False
objHTTP.Send
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write objHTTP.ResponseBody
oStream.SaveToFile (Environ("TEMP") & ".xlsx")
oStream.Close
Workbooks.Open Environ("TEMP") & ".xlsx"
End Sub