Instantly share code, notes, and snippets.

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.writetext cText
'Save binary data To disk
fsT.SaveToFile tFilePath, 2
GoTo finish
MsgBox (Err.Description)
writeOut = 0
Exit Function
writeOut = 1
End Function

This comment has been minimized.

AlexiaChen commented Nov 24, 2015


This comment has been minimized.

sjzlei1989 commented Jul 5, 2016

It works fine, thank you!

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