Results 1 to 22 of 22

Thread: [RESOLVED] ADODB.Stream auto-detect charset problem

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Resolved [RESOLVED] ADODB.Stream auto-detect charset problem

    Sorry, I'm bit annoying with this threads but I wouldn't ask if I'm able to solve that by myself... :/

    I have a problem now with reading files that are in different encodings. I have found a pretty simple solution with ADODB.Stream object, but the problem is that it doesn't work good with detecting between ANSI/UTF-8 and Unicode charsets.

    Module code:
    VB Code:
    1. Private Sub Main()
    2.   With CreateObject("ADODB.Stream")
    3.     .Open
    4.     .LoadFromFile "1.txt"
    5.     MsgBox .ReadText
    6.   End With
    7. End Sub
    8.  
    9. Function MsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String) As VbMsgBoxResult
    10.   MsgBox = CreateObject("WScript.Shell").Popup(Prompt, 0&, Title, Buttons)
    11. End Function
    File "1.txt", Unicode: Сампле тест - or if ANSI: Sample test

    The above example works if the file is saved in Unicode, but when I save "1.txt" in UTF-8 or ANSI, the result from ADODB.Stream will be corrupted. It appears that I need to manually specify charset type in the stream if it isn't Unicode (I have also tried to put .Charset = "_autodetect" but it won't work), so how to do that automatically since I don't know which file will be loaded (it depends on user)? Also, I have read that in some cases BOM doesn't exist in the file but nevertheless programs like Notepad read that files correctly.
    Last edited by MikiSoft; Feb 25th, 2015 at 07:03 PM.

Tags for this Thread

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