0

I'm currently creating a program that downloads URL links as PDF and stores it into my local storage. The problem is that, when it encounters a duplicate name then it overwrites. Any thoughts on instead of overwriting the existing file, It should just add (2) on the end.

My code here is

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
      Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, _
      ByVal szURL As String, ByVal szFileName As String, _
      ByVal dwReserved As Long, ByVal lpfnCB As LongPtr) As Long

Sub DownloadFile()

Dim downloadStatus As Variant
Dim Url As String
Dim destinatioFile_Local As String
Dim i As Integer

i = 1

Do Until IsEmpty(Cells(i, 1)) = True
Url = (Cells(i, 1))
destinationFile_local = "C:\Users\name\Desktop\test" & filename(Cells(i, 1))
downloadStatus = URLDownloadToFile(0, Url, destinationFile_local, 0, 0)
If downloadStatus = 0 Then
    MsgBox "Downloaded"
    Else
    MsgBox "Download fail"
End If
i = i + 1
Loop


End Sub

Function filename(file_fullname) As String

filename = Mid(file_fullname, InStrRev(file_fullname, "/") + 1)
End Function
  • How about checking if the file already exists? See e.g. https://stackoverflow.com/questions/16351249/vba-check-if-file-exists – Shrotter Aug 01 '22 at 09:55
  • Does this answer your question? [VBA save the file on desktop, if name already taken add a numer to it](https://stackoverflow.com/questions/61751680/vba-save-the-file-on-desktop-if-name-already-taken-add-a-numer-to-it) – braX Aug 01 '22 at 10:01

0 Answers0