Vbs Open Txt - Save It with Utf-8 Encoding

Hi I am sorry I cannot figure it out. I tried opening a txt changing its charset to UTF8 and save it under the same filename but that does not work.

Dim Dateisystem, Textdatei, text
Set Dateisystem = CreateObject("Scripting.FileSystemObject")
Set Textdatei = Dateisystem.OpenTextFile("titel.txt")
text = Textdatei.ReadAll


Const adModeReadWrite = 3
Const adTypeText = 2
Const adSaveCreateOverWrite = 2

Sub SaveToFile(text, filename)
  With CreateObject("ADODB.Stream")
    .Mode = adModeReadWrite
    .Type = adTypeText
    .Charset = "UTF-8"
    .Open
    .SaveToFile filename, adSaveCreateOverWrite
    .Close
  End With
End Sub

SaveToFile text, "titel.txt"
1

1 Answer

Try This :

Set stream = CreateObject("ADODB.Stream")
Set fso = CreateObject("Scripting.FileSystemObject")
stream.Open
stream.Type = 2 'text
stream.Charset = "utf-8"
stream.LoadFromFile "C:\Your-File-Here(Input).txt"
text = stream.ReadText
stream.Close
Set f = fso.OpenTextFile("C:\Your-File-Here(Output).txt", 2, True, True)
f.Write text
f.Close
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest