Write Unicode text using VBA


There are some ways to create and access Unicode files using VBA. The most straightforward way is to use the Microsoft Scripting Runtime library.
The two following ways may be used to open a file for write operations:
'the scripting lib way
Dim fs As New FileSystemObject, txt As TextStream
Dim pth As String : pth = "d:\data.txt"
Set txt = fs.CreateTextFile(pth, True, True)
''alternative way to open the textfile
'Set txt = fs.OpenTextFile(pth, ForWriting, True, TristateTrue)

'to write simply write the following in Unicode
txt.WriteLine "test text"

If we want to avoid using the Microsoft Scripting Runtime, we may use the following code; we access the output file in binary mode and we manually convert text to Unicode:
'the manual way
Open pth For Binary Access Write As #1
Dim buffer() as Byte : buffer = "test text"
Put #1, , buffer
Close #1

Comments

Popular posts from this blog

Calling Fortran intrinsic functions from Visual Basic

Dictionary class extensions (CopyTo, Sort) (C#)