VBA-UTF-8文件的操作
阅读原文时间:2023年07月13日阅读:1

1.读入UTF-8文件

   Dim value As String
   Dim vLines As Variant

Call ReadCsvUTF_8(vFile.Path, value)

vLines = Split(value, vbCrLf)

For i = 0 To UBound(vLines)

     …

   next

  Public Sub ReadCsvUTF_8(ByVal fileName As String, ByRef value As String)
    With CreateObject("ADODB.Stream")
        .Charset = "UTF-8"
        .Open
        .LoadFromFile fileName
        value = .ReadText
        .Close
     End With
  End Sub

2.写入UTF-8文件

  Call WriteCsvUTF_8(fileSaveName, strLine)

  Public Sub WriteCsvUTF_8(ByVal fileName As String, ByVal strLine As String)

With CreateObject("ADODB.Stream")

.Open
      .Charset = "UTF-8"
      .Type = adTypeText
      .writeText strLine
      .Position = 3
      .SaveToFile fileName, adSaveCreateOverWrite
      .Flush
      .Close

End With

End Sub