-3

I want to run a VBScript macro to download a set of images from an URL which can de sorted by the key , (comma). I have to name each image with names given in the secondary column. For example: I have 2 columns and 5 rows. In column "A" I have all the names of the images and in column "B" I have all the URL links which can be sorted by the ,. Now I want to download all the images with their names in column "A" and for the second set of images it should rename column "A" by adding 2 at the end of each row, and then it should start downloading the second set of images. Same should go for the 3rd set or 4th set until the image set ends. Sometimes there might be only one image URL in column "B".

Here is the script which I tried to download but I was not able to sort the images and download it by renaming it again.

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
Martha
  • 3,932
  • 3
  • 33
  • 42
Jai
  • 11
  • 4
  • 4
    Welcome to Stack Overflow. This is not a code writing service. We're more than happy to help, but we expect you to make an effort to solve the problem yourself first. It might be a good idea to take the [tour] and read through the [help] pages, particularly [ask], before your next post here. Good luck. – Ken White Jun 20 '17 at 00:25
  • Thank you for the reply. I can download the first set of images with this script but could not able to sort it out with the "," and download second set. Could you help on that? Here is the script which I tried. – Jai Jun 20 '17 at 00:38
  • The code you tried needs to be added to your question with an [edit], not posted as answers. The space below is for answers only, which is why heading for that space is **Your Answer**. – Ken White Jun 20 '17 at 00:47
  • Could you help me on this? – Jai Jun 20 '17 at 00:50
  • Is your code VB.NET or VBScript? – GTAVLover Jun 20 '17 at 03:52
  • As GTAVLover hinted, the sample code you posted is some flavor of VB, but it is not VBScript. – Martha Jun 20 '17 at 05:14
  • @GTAVLover it's VBScript. – Jai Jun 20 '17 at 05:22
  • Here it is https://stackoverflow.com/questions/10383614/get-pictures-from-a-url-and-then-rename-the-picture – Jai Jun 20 '17 at 05:23
  • @Jai Not it isn't..... Please see what tags he used carefully......Who told that you can `Dim` something `As` particular in VBScript? It's only for VB! So `Dim Ret As Long` must be `Dim Ret` here and same applies to other declarations! **See more about this** [**here**](https://stackoverflow.com/questions/12803768/800a0401-expected-end-of-statement) – GTAVLover Jun 20 '17 at 06:12
  • @GTAVLover could you please help me to create a new script to download? And thank you so much for your support. – Jai Jun 20 '17 at 06:55
  • @Jai See my answer and you're good to go. – GTAVLover Jun 20 '17 at 08:34

1 Answers1

1

VBScript is not VB, all variables in VBScript are automatically of type Variant and does not directly support API's. It utilizes COM objects instead.

You have to implement a new function that does the same like URLDownloadToFile API call from urlmon.dll.

This should work:

Function URLDownloadToFile(szURL, szFileName, OverWrite)

  On Error Resume Next

  Dim FSO: Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
  Dim ADO_STREAM: Set ADO_STREAM = WScript.CreateObject("ADODB.Stream")
  Dim HTTP: Set HTTP = WScript.CreateObject("Microsoft.XMLHTTP")

  HTTP.Open "GET", CStr(szURL), False
  HTTP.Send

  If Err.Number <> 0 Then
    WScript.Echo "An error has occured, Not connected to a network" + VbCrLf + "Error " + CStr(Err.Number) + ", " + CStr(Err.Description)
    Err.Clear
    URLDownloadToFile = CInt(-1)
    Exit Function
  End If

  With ADO_STREAM
       .Type = 1
       .Open
       .Write HTTP.ResponseBody
       .SaveToFile szFileName, (CInt(OverWrite) + 1)
  End With

  If Err.Number <> 0 Then
    WScript.Echo "URLDownloadToFile failed, Error " + CStr(Err.Number) + VbCrLf + CStr(Err.Description)
    Err.Clear
    URLDownloadToFile = CInt(-1)
    Exit Function
  End If

  If (Err.Number = 0) And (FSO.FileExists(szFileName) = True) Then
    URLDownloadToFile = CInt(0)
  End If

  On Error Goto 0

End Function

Usage of this function:

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

About OverWrite parameter:

  1. Valid values: 0 or 1
  2. 1 overwrites existing file and 0 creates a new file if file doesn't exist.

If the file downloaded successfully, above function returns 0 and otherwise it returns -1 (In case any error).

Define following, so you can get the last row in Excel.

'~~> Define xlUp
Const xlUp = -4162

You must create an object referring Excel Application like:

Dim Excel: Set Excel = WScript.CreateObject("Excel.Application")

Use Excel.Sheets, instead of using only Sheets in VB. Example:

Set ws = Excel.Sheets("Sheet1")

IMPORTANT: Change your code as applicable.

Dim Ret

'~~> This is where the images will be saved.
Const FolderName = "E:\TEST\"

Sub Sample()
    Dim ws, LastRow, i, strPath

    '~~> Name of the sheet which has the list
    Set ws = Excel.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(ws.Range("B" & i).Value, strPath, 1) '<~~ 1 to overwrite existing file

        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
End Sub
GTAVLover
  • 1,407
  • 3
  • 22
  • 41
  • I don't have knowledge on creating all these things. Could you please help me creating it as an new excel macros? Please guide me how to create all these as an macro in excel. thanks you so much – Jai Jun 20 '17 at 13:14
  • @Jai to do so, you must have at least a basic knowledge about VBScript, You can learn online also, or hire someone. Then what did you mean by *"all these things"* ? – GTAVLover Jun 20 '17 at 13:40