VBA从URL下载文件(使用Ecxel中的URL列表)并将其保存在新文件夹中,文件夹名称也来自excel中的列表

dgenwo3n  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(250)

我需要帮助一个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

我尝试了不同的方法,一个认为我无法实现的是下载文件在正确的文件夹。

c3frrgcw

c3frrgcw1#

Option Explicit

Sub downloadFile()

    Const FOLDER = "D:\SF\"
    
    Dim fso As Object, ws As Worksheet, wb As Workbook
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not (fso.FolderExists(FOLDER)) Then MkDir FOLDER

    Dim oWinHttp As Object, oStream
    Dim URL As String, FilePath As String
    Dim n As Long, r As Long
    
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Tabelle1")
    Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
        
    r = 7
    FilePath = ws.Cells(r, "A")
    Do While Len(FilePath) > 0
    
        If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"

        ' check folder exists
        If Not fso.FolderExists(FOLDER & FilePath) Then
             'Debug.Print FOLDER & FilePath
             MkDir FOLDER & FilePath
        End If

        URL = ws.Cells(r, "D").Value
        oWinHttp.Open "GET", URL, False
        oWinHttp.Send
        If oWinHttp.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            With oStream
                .Open
                .Type = 1
                .Write oWinHttp.ResponseBody
                .SaveToFile FOLDER & FilePath & "File " & r, 2 ' 1 = no overwrite, 2 = overwrite
                .Close
            End With
            n = n + 1
        Else
            MsgBox URL, vbExclamation, "Status " & oWinHttp.Status
        End If

        r = r + 1
        FilePath = ws.Cells(r, "A")
       
    Loop
    MsgBox n & " files created", vbInformation
   
End Sub

相关问题