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 recorder) :
Sub Macro2()
Workbooks.OpenText Filename:= _
"C:/myFile.csv", Origin _
:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False
End Sub
All of the characters (vietnamese characters) are displayed correctly.
When I try the same macro but instead of giving the local address of the file ("C:/myFile.csv") I pass the URL of the file ("http://myserver.com/myFile.csv") the CSV is correctly imported into my Excel sheet but the vietnamese characters are not displayed correctly anymore.
I have also tried using the Data tab but the encoding seems be ignored by Excel:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:/myFile.csv" _
, Destination:=Range("$A$1"))
.Name = "myFile.csv"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "~"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Sample data: „; Â; ˜; Â1/4; ‰; ™,™
which Excel reads wrongly as: „; Â; ˜; Â1/4; ‰; ™,™;
Step 1) Open an Excel-based Macro and access the developer option of excel. Step 2) Select Visual Basic option under Developer ribbon. Step 3) Insert a new module. Step 5) Access the reference option under the tool tab and reference Microsoft HTML Object Library and Microsoft internet control.
Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With