Use “ADODB.Stream” to convert ANSI to UTF-8, miss

2020-04-29 16:56发布

问题:

I need to convert "ANSI" csv file to "UTF-8" csv file. Below code can work, but the first character miss Please see attached screen shot, the original file : Customer the output file : 﨏ustomer

Function Convert(myFileIn, myFileOut)
  Dim stream ,strTextText
  Set stream = CreateObject("ADODB.Stream")

  stream.Open
  stream.Type = 2 'text
  stream.LoadFromFile myFileIn
  stream.Position = 0
  stream.Charset = "gb2312"
  strText = stream.ReadText
  stream.Close

  stream.Open
  stream.Type = 2
  stream.Position = 0
  stream.Charset = "utf-8"
  stream.WriteText strText
  stream.SaveToFile myFileOut, 2
  stream.Close
  Set stream = Nothing

End Function

回答1:

You have to set

stream.Type

and

stream.Charset

before you open the stream.

And stream.Position is 0 by default.

Greetings

Axel



标签: excel vba utf-8