【问题标题】:Specify encoding when Open for input VB为输入 VB 打开时指定编码
【发布时间】:2014-02-27 04:17:16
【问题描述】:

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

Open tmpNmFichier For Input Shared As #iFnum

问题是我的文件有一些字符,例如:“é”、“à”......当我使用 :

解析文件时
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

我的字符“é”...被转换为:“è”或“Ã...”

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

【问题讨论】:

    标签: file vba encoding


    【解决方案1】:

    改用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
    

    价值观

    TristateUseDefault -2 Opens the file using the system default. 
    TristateTrue -1 Opens the file as Unicode. 
    TristateFalse  0 Opens the file as ASCII. 
    

    【讨论】:

    • 附加提示:如果你使用VBA来保存使用这种方法,那么你也应该使用它来阅读。我遇到过使用fso.OpenTextFile TriStateTrue 保存但使用Line Input 读取的情况,并且文件中的前两个字符被导入为ÿþÿþUTF-16 Little Endian 编码文件的 byte order mark。这是一种表示它是 Unicode 文件的方式,而 VBA Line Input 并不知道这一点。使用 fso 方法读取 TriStateTrue 解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2023-04-01
    相关资源
    最近更新 更多