VBA非登陆下载Excel文件并处理例子

2018-06-20 16:20:14 浏览数 (1)

代码语言:javascript复制
'下载文件相关函数申明
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
Public Declare Function DeleteUrlCacheEntry Lib "wininet" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
Sub down()
    nUrl = "下载链接"
    localFilename = ThisWorkbook.Path & "文件名.拓展名"
    lngRetVal = URLDownloadToFile(0, nUrl, localFilename, 0, 0)
    If Dir(localFilename, 16) <> Empty Then '文件存在时执行
        DeleteUrlCacheEntry nUrl '清除缓存
        Set wb = Workbooks.Open(localFilename) '打开文件
        '业务逻辑代码
        wb.Close 0 '关闭文件0不保存
        Kill localFilename '删除文件
    End If
End Sub
'64位兼容参考https://msdn.microsoft.com/library/ee691831.aspx

0 人点赞