Unicode string to flat file from vba

前端 未结 3 823
-上瘾入骨i
-上瘾入骨i 2021-01-20 03:08

I want to store a unicode string in a flat file on a windows box from an excel/vba macro. The macro converts normal string to unicode representation, need to store it in a f

3条回答
  •  不思量自难忘°
    2021-01-20 03:49

    As mentioned, you can use the Microsoft Scripting Runtime (scrrun.dll). I have posted some examples below. Some people also like the native file IO features. There is an extensive (and fairly comprehensive thread) thread here: http://www.xtremevbtalk.com/showthread.php?t=123814

    However for Unicode files it's probably the least painful to use Textstreams:)

    Public Sub StringToTextFile(ByVal path As String, ByVal value As String)
        'Requires reference to scrrun.dll
        Dim fso As Scripting.FileSystemObject
        Dim ts As Scripting.TextStream
        Set fso = New Scripting.FileSystemObject
        Set ts = fso.CreateTextFile(path, False, True)
        ts.Write value
        ts.Close
    End Sub
    
    Public Sub LazyMansWay(ByVal path As String, ByVal value As String)
        'Reference counting will cause the objects to be destroyed. The termination
        'events of the classes will cause the connections to be closed.
        CreateObject("Scripting.FileSystemObject").CreateTextFile(path, False, True).Write value
    End Sub
    

提交回复
热议问题