Download Bilder aus der url und speichern Sie in einem Ordner mit dem Namen in einer Zelle

Ich habe ein Arbeitsblatt mit Ordner-Namen, Bild, Namen und urls. Ich will download jedes Bild, um die bestimmten Ordner, so dass das Ergebnis würde dann so Aussehen:

Folder Name   Image Name     URL
-----------   ------------   -----------------------------------
folder1      image1         http://www.example.com/example1.jpg
folder2      image2         http://www.example.com/example2.jpg
folder3      image3         http://www.example.com/example3.jpg
folder4      image4         http://www.example.com/example4.jpg
folder5      image5         http://www.example.com/example5.jpg

C:\images\folder1\image1.jpg
C:\images\folder2\image2.jpg
C:\images\folder3\image3.jpg
C:\images\folder4\image4.jpg
C:\images\folder5\image5.jpg

Ich habe festgestellt, das VBA-code und es funktioniert wie ein Charme, aber ich weiß nicht, wie eine Methode zum hinzufügen creat Ordner, wenn Sie nicht existieren:

Option Explicit

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

Dim Ret As Long

'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\Temp\"

Sub Sample()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim strPath As String

    '~~> Name of the sheet which has the list
    Set ws = Sheets("Sheet1")

    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To LastRow '<~~ 2 because row 1 has headers
        strPath = FolderName & ws.Range("A" & i).Value & ".jpg"

        Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)

        If Ret = 0 Then
            ws.Range("C" & i).Value = "File successfully downloaded"
        Else
            ws.Range("C" & i).Value = "Unable to download the file"
        End If
    Next i
End Sub

InformationsquelleAutor Jacek Michalski | 2014-05-19

Schreibe einen Kommentar