I have a UTF-8 CSV file stored on a web server. When I download the file put it on my hard drive and I then import it into an Excel sheet with this macro (from the macro recorde
If the characters are displayed correctly when you download the csv
file yourself, I'd divide the process to 2 stages:
Sub DownloadFile(ByVal url As String, ByVal local As String)
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", url, False, "username", "password"
WinHttpReq.send
myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile local, 2
oStream.Close
End If
End Sub
Sub OpenCsv(ByVal csvfile As String)
Workbooks.OpenText Filename:= _
csvfile,Local:=True,StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False
End Sub
Note That: The Local
parameter is the key here,it makes VBA
use your excel's local configuration (vietnamese), which is by default set to False
.
Sub DownloadAndLoad
DownloadFile "http://myserver.com/myFile.csv","C:\myFile.csv"
OpenCsv "C:\myFile.csv"
End Sub