Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[VBA] Write a UTF-8 Encoded File
'Function saves cText in file, and returns 1 if successful, 0 if not
Public Function writeOut(cText As String, file As String) As Integer
On Error GoTo errHandler
Dim fsT, tFilePath As String
tFilePath = file + ".txt"
'Create Stream object
Set fsT = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
fsT.Type = 2
'Specify charset For the source text data.
fsT.CharSet = "utf-8"
'Open the stream And write binary data To the object
fsT.Open
fsT.writetext cText
'Save binary data To disk
fsT.SaveToFile tFilePath, 2
GoTo finish
errHandler:
MsgBox (Err.Description)
writeOut = 0
Exit Function
finish:
writeOut = 1
End Function
@AlexiaChen

This comment has been minimized.

Show comment
Hide comment
@sjzlei1989

This comment has been minimized.

Show comment
Hide comment
@sjzlei1989

sjzlei1989 Jul 5, 2016

It works fine, thank you!

It works fine, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment