简体   繁体   English

在 Excel VBA 中打开下载的文件

[英]Open a downloaded file in Excel VBA

I am using the following code to download a .xlsx file from web on Excel VBA.我正在使用以下代码在 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

The code succeeds to download the file and saves it in the directory.代码成功下载文件并将其保存在目录中。 But instead of saving it I wish I could open the xlsx file on Excel.但我希望我可以在 Excel 上打开 xlsx 文件,而不是保存它。 Is it possible?是否可以?

Set wb = WorkBooks.open( _
  "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx")

So after some research instead of trying to opening the file directly from the memory I just saved it on the user's default temp path and then opened it.因此,经过一些研究,而不是尝试直接从内存中打开文件,我只是将其保存在用户的默认临时路径中,然后将其打开。 Here is the code:这是代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM