|
-
Mar 17th, 2011, 01:24 PM
#1
Thread Starter
Addicted Member
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 ! ! !
-
Mar 17th, 2011, 01:47 PM
#2
Lively Member
Re: Save txt file in Unicode to UTF-8
Have you tried using 1 instead of 2 here?
-
Mar 17th, 2011, 02:11 PM
#3
Thread Starter
Addicted Member
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
-
Mar 17th, 2011, 02:36 PM
#4
Lively Member
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..."?
-
Mar 17th, 2011, 02:40 PM
#5
Thread Starter
Addicted Member
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 ! ! !".
-
Mar 17th, 2011, 02:41 PM
#6
Lively Member
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?
-
Mar 17th, 2011, 02:43 PM
#7
Thread Starter
Addicted Member
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|