简体   繁体   中英

Download images from a CSV / XLS catalog

EDIT: I found this code to download images from an xls file. Source: GET pictures from a url and then rename the picture

Code:

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

It works fine with xls files, where "PIC NAME" and "URL" are in two separate columns. I have a csv file and, unlike xls, csv has it's values in one column separated by "|" (PIC NAME|URL). So - being a noob at coding - as a workaround, I used "Text to columns" in Excel to divide the lines into 2 separate columns right down the delimiter "|", that way I can use the original code.

QUESTION: is there a way to make the code work with a csv file? This would make it much faster and easier to extract images from big csv files with thousands of lines.

Since VBA provides automation for Excel everything that you can do manually also can be automated by VBA. Excel even has a macro recorder integrated that automatically converts all user action into VBA code. This answer explains how it works: Excel's Macro-Recorder usage

This can be used to see what kind of VBA code the Text to columns function generates. If you clean up the code a bit you end up with this VBA command:

Selection.TextToColumns Destination:=Range("A1") _
                      , DataType:=xlDelimited _
                      , Other:=True _
                      , OtherChar:="|"

Make sure that you replace Selection by the whole range that you imported to Excel or that you select the whole range before you execute the code. Also replace Range("A1") by the range where you want the result to be written to.

It should be noted though that it is not very efficient to write stuff into excel cells just to parse those cells afterwards. A cleaner solution to your problem would be to read the downloaded file line by line and parse each line to your needs.

Luckily there is already a solution for that: Read lines from a text file but skip the first two lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM