VBA 判断一个TXT编码方式,再创建一个新的文件,复制数据进去

如题,先读取一个文本文件判断编码(Unicode  ANSI),就这两种编码
然后将txt导入到excel表中,
最后处理完成,再创建一个相同编码,不同文件名的txt文件,把新数据放进去

 

Sub test()
    TxtPath = "D:2.txt"
    '导入excel,执行读取和处理
    ReturnEncoding = GetEncoding(TxtPath)    '获取编码
    If ReturnEncoding = "Unicode" Then
        '====="当前文件是Unicode格式的。"========
        '处理后的excel文件直接另存为"Unicode"编码文件
        MsgBox "Unicode"
        ElseIf ReturnEncoding = "ANSI" Then
        '===== "当前文件是ANSI格式的。"=========
        '新建txt,系统默认就是ANSI格式的,把excel文件写入txt
        MsgBox "ANSI"
    End If
End Sub
Public Function GetEncoding(FileName)
    Dim fBytes(1) As Byte
    Open FileName For Binary Access Read As #1
    Get #1, , fBytes(0)
    Get #1, , fBytes(1)
    Close #1
   GetEncoding = IIf(fBytes(0) = &HFF And fBytes(1) = &HFE, "Unicode", "ANSI")
End Function

原文地址:https://www.cnblogs.com/quanweiru/p/9187291.html