+ Reply to Thread
Results 1 to 3 of 3

Image download loop, cannot resolve IF failed..

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2005
    Posts
    39

    Image download loop, cannot resolve IF failed..

    Greetings

    I'm using the following code to download image files from the web.

    The source of the file and the destination are stored on Sheet1 columns C and D

    I'm stuck on how to trap the success or failure of each loop and color the cell accordingly

    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
     
    Private Const ERROR_SUCCESS As Long = 0
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
     
    Public Function DownloadFile(sSourceURL As String, _
        sLocalFile As String) As Boolean
        DownloadFile = URLDownloadToFile(0&, _
        sSourceURL, _
        sLocalFile, _
        BINDF_GETNEWESTVERSION, _
        0&) = ERROR_SUCCESS
         
    End Function
    
    
    Private Sub CommandButton1_Click()
        Dim sURL As String
        Dim sLocalFile As String
        Dim sDestination As String
        Dim sText As String
        Dim i As Integer
        Dim ws As Worksheet
        
        For i = 4 To 9
        With ws
            Range("B" & i).FormulaR1C1 = "Working...."
            Range("B" & i).Interior.ColorIndex = 6
        
            sText = Sheet1.Range("C" & i).Value
            sURL = sText
            sLocalFile = Sheet1.Range("D" & i).Value
            DownloadFile sURL, sLocalFile
        End With
        'IF successful then
            Range("B" & i).FormulaR1C1 = "Completed"
            Range("B" & i).Interior.ColorIndex = 4
        'Else
        '    Range("B" & i).FormulaR1C1 = "Failed"
        '    Range("B" & i).Interior.ColorIndex = 3
        'End IF      Next i
        
    End Sub
    Last edited by Aurbo; 09-16-2015 at 06:28 PM.

  2. #2
    Registered User
    Join Date
    11-24-2005
    Posts
    39

    Re: Image download loop, cannot resolve IF failed..

    I think I resolved this one myself!

    Can someone confirm please?

    If URLDownloadToFile(0, sURL, sLocalFile, 0, 0) = 0 Then
            Range("B" & i).FormulaR1C1 = "Completed"
            Range("B" & i).Interior.ColorIndex = 4
        Else
            Range("B" & i).FormulaR1C1 = "Failed"
            Range("B" & i).Interior.ColorIndex = 3
        End If
        
        Next i

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Image download loop, cannot resolve IF failed..

    Hi Aurbo,

    Try the following (code excerpt follows):
    Private Sub CommandButton1_Click()
        Dim sURL As String
        Dim sLocalFile As String
        Dim sDestination As String
        Dim sText As String
        Dim i As Integer
        Dim ws As Worksheet
        
        For i = 4 To 9
          With ws
            Range("B" & i).FormulaR1C1 = "Working...."
            Range("B" & i).Interior.ColorIndex = 6
        
            sText = Sheet1.Range("C" & i).Value
            sURL = sText
            sLocalFile = Sheet1.Range("D" & i).Value
            DownloadFile sURL, sLocalFile
          End With
          If LJMFileExists(sLocalFile) = True Then
            Range("B" & i).FormulaR1C1 = "Completed"
            Range("B" & i).Interior.ColorIndex = 4
          Else
            Range("B" & i).FormulaR1C1 = "Failed"
            Range("B" & i).Interior.ColorIndex = 3
          End If
        
        Next i
        
    End Sub
    
    Private Function LJMFileExists(sPathAndFullFileName As String) As Boolean
      'This returns TRUE if a file exists and FALSE if a file does NOT exist
      
      Dim iError As Integer
      Dim iFileAttributes As Integer
    
      On Error Resume Next
      iFileAttributes = GetAttr(sPathAndFullFileName)
         
      'Check the internal error  return
      iError = Err.Number
      Select Case iError
        Case Is = 0
            iFileAttributes = iFileAttributes And vbDirectory
            If iFileAttributes = 0 Then
              LJMFileExists = True
            Else
              LJMFileExists = False
            End If
        Case Else
            LJMFileExists = False
      End Select
    
      On Error GoTo 0
    
    End Function
    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Download image from url and save to folder
    By adtastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-09-2015, 05:59 PM
  2. [SOLVED] Image download from links in column A
    By fitkhan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-21-2014, 02:18 PM
  3. Download, Resolve and Chart Multiple Date Series
    By JohnYard in forum Excel General
    Replies: 0
    Last Post: 02-06-2014, 11:56 AM
  4. [SOLVED] How can I resolve this Run-Time error '1004': Method 'Range' of object '_Worksheet' failed
    By dafella in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2013, 01:16 AM
  5. [SOLVED] Download Image to specific directory
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-31-2013, 03:00 PM
  6. Formula: No of days for which contract remained active in a particular month
    By usama.anwar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2012, 02:29 PM
  7. Replies: 2
    Last Post: 12-20-2005, 09:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1