如何从Internet Explorer自动下载文件

时间:2023-01-27 11:49:25

I wanted to download a file from the internet (its a execl file) so I want to use VBA to automate the entire process. I have the address of the file but I just want to download it and then save it. The other thing is that it can only be opened in internet explorer the file cannot be opened in chrome or firefox.

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

1 个解决方案

#1


3  

Use the URLDownloadToFile API function

使用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

#1


3  

Use the URLDownloadToFile API function

使用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