excelvbaでファイルのダウンロード

URLDownloadToFile APIを使用してWebからファイルをダウンロードする方法

#If Win64 Then
Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet" Alias _
        "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias _
        "URLDownloadToFileA" (ByVal pCaller As LongPtr, _
        ByVal szURL As String, _
        ByVal szFileName As String, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As LongPtr) As LongPtr
#Else
Private Declare Function DeleteUrlCacheEntry Lib "wininet" Alias "DeleteUrlCacheEntryW" ( _
        ByVal lpszUrlName As Long) As Long
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileW" ( _
        ByVal pCaller As Long, _
        ByVal szURL As Long, _
        ByVal szFileName As Long, _
        ByVal dwReserved As Long, _
        ByVal lpfnCB As Long) As Long
#End If
Sub aaa()

    Const strURL = "http://www.ken3.org/index.html"
    Dim strFNAME As String  'ダウンロード先(パス+ファイル名)
    Dim returnValue

    'ファイル名をブックのパス+test.htmlとする
    strFNAME = ThisWorkbook.Path & "\test.html"

    'URLDownloadToFile API をコールする
    returnValue = URLDownloadToFile(0, strURL, strFNAME, 0, 0)

    '結果の表示
    MsgBox "結果は:" & returnValue
    MsgBox strFNAME & "に保存されました"
End Sub

ネタ元