How to save a Unicode character to a text file

后端 未结 3 745
眼角桃花
眼角桃花 2020-12-20 02:22

This is in Word for MAC VBA. I want to save the Unicode character from a text box to text file. For example this character \"⅛\".

I use this code.

D         


        
3条回答
  •  猫巷女王i
    2020-12-20 02:46

    VBA can't code text in UTF-8 this way. Use ADODB - yes, for text, not for database.

    'ensure reference is set to Microsoft ActiveX DataObjects library
    '(the latest version of it) under "tools/references"
    Sub AdoTest()
        Dim adoStream As ADODB.Stream
    
        Set adoStream = New ADODB.Stream
    
        'Unicode coding
        adoStream.Charset = "Unicode" 'or any string listed in registry HKEY_CLASSES_ROOT\MIME\Database\Charset
    
        'open sream
        adoStream.Open
    
        'write a text
        adoStream.WriteText "Text for testing: ěšč", StreamWriteEnum.stWriteLine
    
        'save to file
        adoStream.SaveToFile "D:\a\ado.txt"
    
        adoStream.Close
    End Sub
    

    Reading is simplier, see my answer here:

    Unicode and UTF-8 with VBA

    Edited: I've inserted complete example.

    Edited 2: Added refernce to list of coding in the registry

提交回复
热议问题