2010-03-26 281 views
38
编码

我怎么能写UTF-8编码字符串从VBA一个文本,像保存文本文件UTF-8 VBA

Dim fnum As Integer 
fnum = FreeFile 
Open "myfile.txt" For Output As fnum 
Print #fnum, "special characters: äöüß" 'latin-1 or something by default 
Close fnum 

是否有应用层的一些设置?

回答

61

我发现了web答案:

Dim fsT As Object 
Set fsT = CreateObject("ADODB.Stream") 
fsT.Type = 2 'Specify stream type - we want To save text/string data. 
fsT.Charset = "utf-8" 'Specify charset For the source text data. 
fsT.Open 'Open the stream And write binary data To the object 
fsT.WriteText "special characters: äöüß" 
fsT.SaveToFile sFileName, 2 'Save binary data To disk 

肯定不如我的预期......

+0

你好,如果我想保存在utf-16中,我只需要改变8到16的权利? – Smith 2011-01-07 13:06:31

+16

我不知道,你试过吗? – 2011-01-09 18:51:41

+1

无法在Mac上使用 – 2016-07-12 07:53:31

12

您可以使用CreateTextFile或OpenTextFile方法,它们都具有属性“unicode”用于编码设置。

object.CreateTextFile(filename[, overwrite[, unicode]])   
object.OpenTextFile(filename[, iomode[, create[, format]]]) 

示例:覆盖:

CreateTextFile: 
fileName = "filename" 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set out = fso.CreateTextFile(fileName, True, True) 
out.WriteLine ("Hello world!") 
... 
out.close 

例:追加:

OpenTextFile Set fso = CreateObject("Scripting.FileSystemObject") 
Set out = fso.OpenTextFile("filename", ForAppending, True, 1) 
out.Write "Hello world!" 
... 
out.Close 

查看更多关于MSDN docs

+0

有趣。对象是类“FileSystemObject”,对吧?我将如何写入这个文件? '.Write'? – 2012-07-13 13:52:37

+1

'A/CreateTextFile: 文件名= “文件名” FSO设置=的CreateObject( “Scripting.FileSystemObject的”) 设置了= fso.CreateTextFile(文件名,真,真) out.WriteLine( “世界,你好!”) ... out.close b /的OpenTextFile FSO设置=的CreateObject( “Scripting.FileSystemObject的”) 载列= fso.OpenTextFile( “文件名”,ForAppending,TRUE,-1) out.Write“你好,世界!” ... out.Close' – 2012-10-03 08:49:54

+15

Unicode!= UTF-8 – Helen 2014-11-05 09:44:33

5

这里是另一种方式来做到这一点 - 使用API​​函数调用WideCharToMultiByte :

Option Explicit 

Private Declare Function WideCharToMultiByte Lib "kernel32.dll" (_ 
    ByVal CodePage As Long, _ 
    ByVal dwFlags As Long, _ 
    ByVal lpWideCharStr As Long, _ 
    ByVal cchWideChar As Long, _ 
    ByVal lpMultiByteStr As Long, _ 
    ByVal cbMultiByte As Long, _ 
    ByVal lpDefaultChar As Long, _ 
    ByVal lpUsedDefaultChar As Long) As Long 

Private Sub getUtf8(ByRef s As String, ByRef b() As Byte) 
Const CP_UTF8 As Long = 65001 
Dim len_s As Long 
Dim ptr_s As Long 
Dim size As Long 
    Erase b 
    len_s = Len(s) 
    If len_s = 0 Then _ 
    Err.Raise 30030, , "Len(WideChars) = 0" 
    ptr_s = StrPtr(s) 
    size = WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, 0, 0, 0, 0) 
    If size = 0 Then _ 
    Err.Raise 30030, , "WideCharToMultiByte() = 0" 
    ReDim b(0 To size - 1) 
    If WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, VarPtr(b(0)), size, 0, 0) = 0 Then _ 
    Err.Raise 30030, , "WideCharToMultiByte(" & Format$(size) & ") = 0" 
End Sub 

Public Sub writeUtf() 
Dim file As Integer 
Dim s As String 
Dim b() As Byte 
    s = "äöüßµ@€|~{}[]²³\ .." & _ 
    " OMEGA" & ChrW$(937) & ", SIGMA" & ChrW$(931) & _ 
    ", alpha" & ChrW$(945) & ", beta" & ChrW$(946) & ", pi" & ChrW$(960) & vbCrLf 
    file = FreeFile 
    Open "C:\Temp\TestUtf8.txt" For Binary Access Write Lock Read Write As #file 
    getUtf8 s, b 
    Put #file, , b 
    Close #file 
End Sub 
1

我看着Máťa的名字暗示了编码的资格和经验。 VBA docs表示CreateTextFile(filename, [overwrite [, unicode]])创建一个文件“作为Unicode或ASCII文件,如果该文件创建为Unicode文件,则为True;如果创建为ASCII文件,则为False;如果省略,则假定为ASCII文件。文件存储unicode字符是好的,但使用什么编码?未编码的unicode不能在文件中表示。

VBA doc pageOpenTextFile(filename[, iomode[, create[, format]]])提供了格式的第三个选项:

  • TriStateDefault 2“打开使用系统默认的文件。”
  • TriStateTrue 1“以Unicode形式打开文件”。
  • TriStateFalse 0“以ASCII形式打开文件”。

Máťa为此论证传递-1。

VB.NET documentation(不是VBA,但我认为反映了底层Windows操作系统如何代表unicode字符串并回显MS Office的现实情况,我不知道),系统默认是使用1字节/ unicode字符的编码,使用ANSI语言环境的代码页。 UnicodeEncoding是UTF-16。该文档还描述了UTF-8也是一种“Unicode编码”,这对我来说很有意义。但我还不知道如何为VBA输出指定UTF-8,也不知道我用OpenTextFile(,,, 1)写入磁盘的数据是UTF-16编码的。 Tamalek's post是有帮助的。