2010-03-26 280 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是有幫助的。