title: What is a way to automate downloading a picture from a link in Excel/VBA?
body: <p>So here's the situation: I'm trying to download a number of pictures from an external server onto my local computer.</p>
  
  <p>The Excel file has a link to the picture which will open and download the picture. </p>
  
  <p>What I've tried so far is to convert the hyperlinks into just text (of the picture url) and run the following code. </p>
  
  <p>I'm only basically familiar with VBA, more so with other languages though.  Here is the code I have so far:</p>
  
  <pre><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
  
    '~~&gt; This is where the images will be saved. Change as applicable
    Const FolderName As String = "C:\Users\My Name\Downloads\"
  
    Sub DownloadLinks()
  Dim ws As Worksheet
  Dim LastRow As Long, i As Long
  Dim strPath As String
  
  '~~&gt; Name of the sheet which has the list
  Set ws = Sheets("Sheet1")
  
  LastRow = ws.Range("B" &amp; Rows.Count).End(xlUp).Row
  
    For i = 2 To LastRow '&lt;~~ 2 because row 1 has headers
      strPath = FolderName &amp; ws.Range("BP" &amp; i).Value &amp; ".jpg"
  
      Ret = URLDownloadToFile(0, ws.Range("BP" &amp; i).Value, strPath, 0, 0)
  
      If Ret = 0 Then
          ws.Range("CA" &amp; i).Value = "File successfully downloaded"
      Else
          ws.Range("CA" &amp; i).Value = "Unable to download the file"
      End If
  Next i
  
    End Sub
  </code></pre>
  
  <p>The column names are irrelevant, but right now, everything comes out as "Unable to download file" or if it is successful, it isn't in the directory I specified.</p>
  
  <p>Is there a better way to code this? </p>
  
  <p>Something about my data maybe? </p>
  
  <p>I'd also like it to save the file name as text in another column if possible, but that isn't necessary. </p>
  
  <p>Right now I just need them to get downloaded.</p>