Specify encoding when Open for input VB

2019-02-26 04:07发布

问题:

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?

回答1:

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.