我需要帮助一个vba脚本,在一个网址列表,并为每个网址下载一个文件。该文件需要保存在新的文件夹中。文件夹名称也在excel表。
Sub downloadFile()
Dim chromePath As String, hl As Hyperlink
Dim fso As Object
Dim oWinHttp As Object
Dim URL As String, FilePath As String
Sheets("Tabelle1").Range("A7").Activate
Set fso = CreateObject("Scripting.FileSystemObject")
If Not (fso.FolderExists("D:\SF\")) Then MkDir "D:\SF\"
If Not fso.FolderExists("D:\SF\" & ActiveCell.Value) Then MkDir "D:\SF\" & ActiveCell.Value
Do While Not (IsEmpty(ActiveCell.Value))
URL = ActiveCell.Offset(0, 3).Value
FilePath = "D:\SF\" & ActiveCell.Value
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
oWinHttp.Open "GET", URL, False
oWinHttp.Send
If oWinHttp.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write oWinHttp.ResponseBody
oStream.Save
oStream.SaveToFile FilePath, 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If
ActiveCell.Offset(1, 0).Select
If (Not (IsEmpty(ActiveCell.Value)) And ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value) Then
If Not fso.FolderExists("D:\SF\" & ActiveCell.Value) Then MkDir "D:\SF\" & ActiveCell.Value
End If
Loop
End Sub
我尝试了不同的方法,一个认为我无法实现的是下载文件在正确的文件夹。
1条答案
按热度按时间c3frrgcw1#