这个
Open
VBA的功能工作于
ANSI
仅编码文件和二进制文件。如果您希望读/写
utf-8
文件,你必须找到另一种方法。
这个
编码的字符集比
,因此不可能从
ANSI标准
utf-8
String
utf-16
ANSI标准
utf-8
到
.
具有
ADODB.Stream
Public Function ReadFile(path As String, Optional CharSet As String = "utf-8")
Static obj As Object
If obj Is Nothing Then Set obj = VBA.CreateObject("ADODB.Stream")
obj.CharSet = CharSet
obj.Open
obj.LoadFromFile path
ReadFile = obj.ReadText()
obj.Close
End Function
Public Sub WriteFile(path As String, text As String, Optional CharSet As String = "utf-8")
Static obj As Object
If obj Is Nothing Then Set obj = VBA.CreateObject("ADODB.Stream")
obj.CharSet = CharSet
obj.Open
obj.WriteText text
obj.SaveToFile path
obj.Close
End Sub