I am trying to download PDFs from a web link. I have the below code which works when I test it with the below links. However for the website I actually need it to work it doesnt. It downloads a pdf file but then say it cant open the file because it doesnt support the file which doesnt make sense since the link is a pdf. any help is appreciated
http://www.redrobinpa.com/wp-content/uploads/2017/06/0517_FAX_RRLVRG3.pdf
http://www.bigfoto.com/sites/main/tree-winter-xxx.JPG
Sub Test()
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
MyFile = "https://ABC.com/Fund/495adc9d98da4e288d71d602d47261ba.pdf"
WHTTP.Open "GET", MyFile, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
If Dir("C:\MyDownloads", vbDirectory) = Empty Then MkDir "C:\MyDownloads"
FileNum = FreeFile
Open "C:\MyDownloads\test.pdf" For Binary Access Write As #FileNum
Put #FileNum , 1, FileData
Close #FileNum
End Sub
http://www.redrobinpa.com/wp-content/uploads/2017/06/0517_FAX_RRLVRG3.pdf
http://www.bigfoto.com/sites/main/tree-winter-xxx.JPG
Sub Test()
Dim FileNum As Long
Dim FileData() As Byte
Dim MyFile As String
Dim WHTTP As Object
On Error Resume Next
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
If Err.Number <> 0 Then
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
End If
On Error GoTo 0
MyFile = "https://ABC.com/Fund/495adc9d98da4e288d71d602d47261ba.pdf"
WHTTP.Open "GET", MyFile, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
If Dir("C:\MyDownloads", vbDirectory) = Empty Then MkDir "C:\MyDownloads"
FileNum = FreeFile
Open "C:\MyDownloads\test.pdf" For Binary Access Write As #FileNum
Put #FileNum , 1, FileData
Close #FileNum
End Sub