Download File from Url and SaveAs Overwrite if Exists

Hi All,

I have a code which i copied from the different sources, could you please guide on improving this query.

I want to Download a file and save the file in a given path and if the file already exist, it has to overwrite it.

Currently i have code only for downloading.

Code:

Private Sub CommandButton1_Click() 'Need to reference to Microsoft Internet Controls
Dim URL As String
'URL = Worksheets("References & Resources").Range("URLMSL")
URL = "Https://sampleUrl.sss" 'for TEST
Dim IE As Object Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.Navigate URL
SaveAs "D:\Documents and Settings\sss\Desktop\SW\"
Do While IE.ReadyState <> 1
DoEvents Loop
End Sub

Nick's picture

to overwrite an existing

to overwrite an existing file, the easiest way is to delete it at the start using a sub like this:

Sub KillExisting(FilePath)
on error resume next ' don't error if it doesn't exist
kill FilePath
end sub

then save as normal

Hi Nick, Thanks for the

Hi Nick,

Thanks for the advice, however, in the above code which i have written, am unable to save the file in the given path. Could you please help me in saving the file when i download the file from URL