2016-04-06 41 views
1

我使用以下代碼從Excel VBA上的Web下載.xlsx文件。在Excel VBA中打開下載的文件

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文件。可能嗎?

回答

1
Set wb = WorkBooks.open(_ 
    "https://www.arembepe.net/temp/COMDINHEIRO_gabrielzancheta749999241.xlsx") 
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