Results 1 to 7 of 7

Thread: Save txt file in Unicode to UTF-8

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Angry 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 ! ! !

  2. #2
    Lively Member Vanquish2888's Avatar
    Join Date
    Sep 2007
    Posts
    104

    Re: Save txt file in Unicode to UTF-8

    vb Code:
    1. .SaveToFile myPath, 2
    Have you tried using 1 instead of 2 here?

  3. #3

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    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

  4. #4
    Lively Member Vanquish2888's Avatar
    Join Date
    Sep 2007
    Posts
    104

    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..."?

  5. #5

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    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 ! ! !".

  6. #6
    Lively Member Vanquish2888's Avatar
    Join Date
    Sep 2007
    Posts
    104

    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?

  7. #7

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    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
  •  



Click Here to Expand Forum to Full Width