VBA - URLDownloadToFile - File with wrong content

Rafael Nascimento

New Member
Joined
Jan 21, 2018
Messages
1
Hello guys!

I am facing an specifc issue here, I am trying to acess the Airbnb webpage, and then download a CSV file with the historical transactions/reservations.

The interesting thing is that when I use the API function URLDownloadToFile in my code, the CSV file comes filled with a lot of code (HTML).

However, when I click on the link "Export CSV" in the webpage, the file's content comes ok with the right information.


I am searching for information, and until now I didn't get anything.


I would be very thankful for any help! (Sorry for my poor english)


Bellow follows my code with comments.


Option Explicit


#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr) As LongPtr
#Else
Private Declare 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
#End If




Sub BrowseToAirbnb_Wastolfi()


Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.IHTMLElement
Dim HTMLPage As MSHTML.HTMLDocument
Dim Year As Integer
Dim strURL As String
Dim LocalFilePath As String
Dim DownloadStatus As Long
Dim File_name As String


File_name = Range("User").Value
Year = Range("Selection_year").Value


IE.Visible = True
IE.navigate "www.airbnb.com/users/transaction_history"


Do While IE.readyState <> READYSTATE_COMPLETE
Loop


Debug.Print IE.LocationName, IE.LocationURL




Set HTMLDoc = IE.document




HTMLDoc.all.Email.Value = "xxxx@yyyyyy" ' Login
HTMLDoc.all.Password.Value = "zzzzzzz" 'Password


Set HTMLInput = HTMLDoc.getElementById("user-login-btn")
HTMLInput.Click ' Clicking on the button ENTER


Do While IE.readyState <> READYSTATE_COMPLETE
Loop
Application.Wait (Now + TimeValue("0:00:10"))



strURL = "https://www.airbnb.com.br/transaction_history/csv/ccccccccccfor_payout_tracker=true&year=" & Year & "&start_month=1&end_month=12&page=1"
LocalFilePath = "C:\Users\Administrador\Desktop\Test" & File_name & ".csv"
DownloadStatus = URLDownloadToFile(0, strURL, LocalFilePath, 0, 0)

If DownloadStatus = 0 Then

MsgBox "File Downloaded. Check in this path: " & LocalFilePath

Else

MsgBox "Download File Process Failed"

End If



End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi

Did you find a solution?
I have the same problem.
My code has worked fine for several years, but now my source has been moved to a sharepoint server.
Instead of my text file with text, I now receive a text file with html code.
I have the error on windows 7 but on my windows 10 it works without any problems



<form method="POST" name="hiddenform" action="https://[/CODE]<form method=" post"=""

Part of my code:

Code:
Public Function DownloadFile(sSourceUrl As String, sLocalFile As String) As Boolean
Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000



   DownloadFile = URLDownloadToFile(0&, sSourceUrl, sLocalFile, BINDF_GETNEWESTVERSION, 0&) = ERROR_SUCCESS
End Function




sub test
   sSourceUrl = "https://xxx.sharepoint.com/...../Versionkontrol.txt"
   sLocalFile = CurDir() & "\Versionkontrol.txt"
   If DownloadFile(sSourceUrl, sLocalFile) Then
             hfile = FreeFile
            Open sLocalFile For Input As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=hfile"]#hfile[/URL]</form>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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