Save txt file in Unicode to UTF-8
I have a bunch of txt files saved in Unicode and I wanted to create a macro that would convert them to UTF-8. I coded the following conversion function:
Code:
Sub Encode(ByVal myPath As String)
Set objStream = CreateObject("ADODB.Stream")
With objStream
.Charset = "Unicode"
.Open
.LoadFromFile myPath
.Charset = "UTF-8"
.SaveToFile myPath, 2
.Close
End With
Set objStream = Nothing
End Sub
But it's doing weird things. The new files are indeed in UTF-8 format but the conversion also added one space character after every character in the file. S o , I g e t s o m e t h i n g l i k e t h i s ! ! !
Re: Save txt file in Unicode to UTF-8
Have you tried using 1 instead of 2 here?
Re: Save txt file in Unicode to UTF-8
That wouldn't work. Setting the save options to 2 allows you to overwrite.
SaveToFile Method
Re: Save txt file in Unicode to UTF-8
Oh my mistake, I thought it was the part of the stream you were saving to the file.
When you said "I g e t s o m e t h i n g..." Does it give you that exactly or "I __(2 spaces) g e t s o m e t h i n g__l i k e..."?
Re: Save txt file in Unicode to UTF-8
Exactly, it should say "So, I get something like this !!!" and I get "S o , I g e t s o m e t h i n g l i k e t h i s ! ! !".
Re: Save txt file in Unicode to UTF-8
When you have a space character, does it really return 2 space characters or just 1?
Re: Save txt file in Unicode to UTF-8
And yes, two spaces instead of one for actual spaces. One solution is to reopen the file, once converted, replace all double spaces by, for example, @, replace all remaining spaces by "" and finally replace all @s by a space. But this is my last option. I just don't get why it is doing that weird conversion...