如何从Internet Explorer自动下载文件

时间:2015-03-30 19:31:37

标签: excel vba

我想从互联网上下载一个文件(它是一个execl文件),所以我想使用VBA自动完成整个过程。我有文件的地址,但我只想下载然后保存。另一件事是它只能在Internet Explorer中打开,文件无法在chrome或firefox中打开。

1 个答案:

答案 0 :(得分:3)

使用URLDownloadToFile API函数

#If VBA7 Then
    Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
        Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, ByVal szFileName As String, _
        ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#Else
    Private Declare Function URLDownloadToFile Lib "urlmon" _
        Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
        ByVal szURL As String, ByVal szFileName As String, _
        ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If
    Sub Demo()
         URLDownloadToFile 0, _
        "http://www.example.com/myworkbook.xlsx", _
        "C:\users\me\documents\myworkbook.xlsx", 0, 0
    End Sub