PDA

View Full Version : Has anyone got a function to permit reading of a unicode text file?



johndavidson
11-30-2012, 08:47 PM
I have some code that reads an ASCII text file that I'd like to convert to reading a unicode text file. While the FileSystemObject permits writing of Unicode, the fso cannot be used to read it. The neatest solution I can think of would be to replace the existing code with a function that does the same using another method. Various sites suggest reading the file into a byte array, but that is beyond my skills. Has anyone got any code that will do this?

Here's my present piece of code:

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(p_strFilename) = False Then
p_params.FileLists = FileLists
p_params.Selections = Selections
p_params.SearchStrings = SearchStrings
Set fso = Nothing
Exit Sub
End If

Set tsIn = fso.OpenTextFile(p_strFilename) ' this reads an ASCII text file OK

' These result in “Invalid Procedure call or argument” and “Variable not defined”, respectively:

' Set tsIn = fso.OpenTextFile(p_strFilename, , , 1)
' Set tsIn = fso.OpenTextFile(p_strFilename, , , TristateTrue)

Do Until tsIn.AtEndOfStream
strLine = LTrim(tsIn.ReadLine)
strProperty = Left(strLine, 12)
strValue = Mid(strLine, 13)
If Left(strValue, 1) = " " Then
strValue = Mid(strValue, 2)
End If

strValue = Trim(strValue)

Select Case strProperty
Case etc.

Loop

johndavidson
11-30-2012, 09:25 PM
Well ... I have resolved the issue. Apparently, you can read unicode files!! I must have been misundstanding something, or maybe the posts I read were for older versions of VBA and the FileScriptingObject.

It was my implementation of the code that was wrong. The parameters need to be stated explicitly, like:

Set tsIn = fso.OpenTextFile(strFromFile, 1, False, -1)

So far, so good! The code is reading and writing unicode.