Last active
December 14, 2023 10:30
-
-
Save JoBrad/1023484 to your computer and use it in GitHub Desktop.
[VBA] Write a UTF-8 Encoded File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 As Object | |
Dim 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 |
It works fine, thank you!
Code works with one small correction:
Old:
Dim fsT, tFilePath As String
New
Dim fsT As Object
Dim tFilePath As String
Regards
@Nomaxx, updated. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thats very nice I found out it on Stackoverflow http://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba