vba 打开输入 VB 时指定编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10996922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:28:16  来源:igfitidea点击:

Specify encoding when Open for input VB

filevbaencoding

提问by Thoma Bigueres

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

我正在尝试使用以下行打开一个文件以在 Excel 中输入:

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?

你知道我如何明确我的文件的编码或类似的东西吗?

回答by Cylian

Use FileSystemObjectinstead

使用FileSystemObject替代

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.