4

I'm trying to open a file for input in Excel with this line :

Open tmpNmFichier For Input Shared As #iFnum

The problem is that my file has some characters like : "é", "à" ... When I'm parsing the file with :

Dim s As String

Line Input #iFnum, s
If Len(s) > 0 Then
     v = Split(s, tmpSeparateur)
Else
    ReDim v(-1 To -1)
End If

my characters "é" ... are transformed in : "è" or "Ã..."

Do you have any idea how i can explicit the encoding of my file or something like that?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Thoma Bigueres
  • 141
  • 1
  • 5
  • 14

1 Answers1

6

Use FileSystemObject instead

Public Function GetContent(fpath$) As String
    'REFERENCES:
    'Microsoft Scripting Runtime // Scripting.FileSystemObject
    Dim fso As New Scripting.FileSystemObject, content$
    If fso.FileExists(fpath) Then
        content = fso.OpenTextFile(fpath, ForReading, False, TristateTrue).ReadAll()
        GetContent = content
    Else
        MsgBox "Invalid file path."
        GetContent = vbNullChar
    End If
End Function

values

TristateUseDefault -2 Opens the file using the system default. 
TristateTrue -1 Opens the file as Unicode. 
TristateFalse  0 Opens the file as ASCII. 
Cylian
  • 10,970
  • 4
  • 42
  • 55
  • **Additional tip:** If you use VBA to save using this method then you should also use it to read. I had a situation where I was saving with `fso.OpenTextFile TriStateTrue` but reading with `Line Input`, and the first two characters in the file were imported as `ÿþ`. `ÿþ` is the [byte order mark](https://en.wikipedia.org/wiki/Byte_Order_Mark) of a [UTF-16](https://en.wikipedia.org/wiki/UTF-16) Little Endian encoded file. That is a way to signal it is a Unicode file and the VBA *Line Input* wasn't aware of that. Using the fso method to read with TriStateTrue solved my problem. – Ben Sep 25 '19 at 12:03