URLDownloadToFile file type issues

lmvk99

Board Regular
Joined
Dec 15, 2011
Messages
68
Hi guys,

I have mish-mashed the following code together to go and grab an excel file from a website, download it and rename it.

Code:
Sub DownloadFilefromWeb()    Dim strSavePath As String
    Dim URL As String, ext As String
    Dim buf, ret As Long
    For Each cell In Range("new_url")
    If cell.Value = "" Then Exit Sub
    URL = cell.Value
    buf = Split(URL, ".")


    strSavePath = ThisWorkbook.Path & "\" & "DownloadedFile" & cell.Row - 1 & ".xls"
    ret = URLDownloadToFile(0, URL, strSavePath, 0, 0)
    Next
End Sub

The problem is I keep getting errors about file types. When i manually download the file I can see it is in .xls format. but then I use that in the code and then try and open my downloaded files i get the error "The file you are trying to open "DownloadedFile.xls", is in a different format than specified by the file extension. verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open now?" I say yes and I get a corrupted file. yet if I manually download the same file it is fine. If I try any other file extension type I get the error "Excel found unreadable content in 'DownloadedFile1.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook click yes". I click yes and it tells me the file cannot be openned. Finally I tried the code with no extension. The file appears in the folder as expected by when i try and open with excel, nothing happens.

Any ideas?

The complicating factor is that the file extension may change so I'd rather not specify if at all possible. Note that the URL does not contain the file extension.

Thanks in advance!!!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,216,735
Messages
6,132,422
Members
449,727
Latest member
Aby2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top