FatalLordes
Board Regular
- Joined
- Dec 22, 2017
- Messages
- 78
- Office Version
- 365
- Platform
- Windows
Hi all. I have the below code which is set to download the file from the URL and rename the file with the name in C and append the time in HHMMSS at the end as likely there will be more than one file for the name in C and I didn't know how to add on a number like NAME02 if NAME01 exists, etc. I think this is the issue but I could be wrong because the issue is the script works fine when I F8 step through the script, getting the multiple files for the same name, but it only gets one file for the name if I just run the script (because I assume it runs that fast the seconds haven't had a chance to click over). If my logic is right and it is running too quick, could someone perhaps point me as to how I would append a number to the name if the prior number exists (eg., if NAME01 exists, go to NAME02, etc). Or is there some other way to do it? Thanks in advance.
VBA Code:
Option Explicit
Private Declare PtrSafe 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:\PPF\CatMedical\"
Sub DownloadCatMedical()
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("Medical")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow '<~~ 2 because row 1 has headers
strPath = FolderName & ws.Range("C" & i).Value & Format(Time, "hhmmss") & ".jpg"
Ret = URLDownloadToFile(0, ws.Range("I" & i).Value, strPath, 0, 0)
If Ret = 0 Then
ws.Range("Z" & i).Value = "File successfully downloaded"
Else
ws.Range("Z" & i).Value = "Unable to download the file"
End If
Next i
End Sub