Reading Unicode Text File

N

NorCalVikesFan

We have an application that does mail merges using a macro which we are
trying to make internationally aware. The macro needs to be able to read a
Unicode text file and parse the text from the lines. A line in the text
file will look something like this:

"Start Date", "XSTARTDATE"
"End Date", "XENDDATE"

The first "field" is user definable, so it can be any characters the user
chooses.

Does anyone have any sample code that might do something like this?
Currently we just use the standard syntax like this:

Open "myfile.txt" For Input as 1
Input #1, sCaption, sField

But this doesn't seem to work when myfile.txt is a Unicode text file.

Any help you can give me would be greatly appreciated.
 
K

Klaus Linke

Not 100% sure, but I don't think you can make Input # work with Unicode text
files.

You'll probably have to read the file as Binary, and then parse it yourself:

Dim sCaption As String, sField As String
Dim iFile As Integer, i As Integer
Dim strFile As String
Dim vFile As Variant
iFile = FreeFile
Open "myFile.txt" For Binary As iFile
strFile = Space(LOF(iFile) - 1)
Get #iFile, 3, strFile
Close #iFile
strFile = StrConv(strFile, vbFromUnicode)
strFile = Replace(strFile, Chr(10), "")
strFile = Replace(strFile, """,", """" & vbCr)
vFile = Split(strFile, vbCr)
For i = LBound(vFile) To UBound(vFile) - 1
vFile(i) = Trim(vFile(i))
vFile(i) = Mid(vFile(i), 2, Len(vFile(i)) - 2)
Next i
For i = LBound(vFile) To UBound(vFile) - 1 Step 2
MsgBox vFile(i), , vFile(i + 1)
Next i

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top