0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel経由でWin32APIのURLDownloadToFileを使ってファイルをダウンロードするVBScript

Last updated at Posted at 2019-02-22

概要

Excelがあれば、Win32APIのURLDownloadToFile()を呼び出して、web上のファイルをダウンロードします。
ExcelのApplication.ExecuteExcel4Macro()メソッドを利用しています。

使用方法

  • InputBoxからURLを入力
  • 「名前を付けて保存」ダイアログから、保存先のファイル名を入力
  • キャンセルするまで繰り返す
URLDownloadToFile.vbs
Option Explicit
Dim Application
Dim CD
Dim URL
Dim Path
Dim RC

Set Application=CreateObject("Excel.Application")
CD=CreateObject("Scripting.FileSystemObject").GetAbsolutePathName("")
Call Application.ExecuteExcel4Macro("CALL(""kernel32"",""SetCurrentDirectoryA"",""JC"","""&CD&"¥..¥"")")
Do
  URL=InputBox("URLを入力してください。")
  If URL="" Then Exit Do
  Path=Application.GetSaveAsFileName("*.*","すべてのファイル,*.*")
  If Path=False Then Exit Do
  RC=Application.ExecuteExcel4Macro("CALL(""urlmon"",""URLDownloadToFileA"",""JJCCJJ"",0,"""&URL&""","""&Path&""",0,0)")
  If RC Then
    MsgBox Join(Array(URL,Path,Hex(RC)),vbLf),vbCritical,"失敗"
    Err.Raise &H80070000 Or (&HFFFF& And RC)
  Else
    MsgBox Join(Array(URL,Path,RC),vbLf),vbInformation,"成功"
  End If
Loop
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?