ANSII中的特殊字符通过VBA

时间:2015-08-04 08:46:18

标签: excel vba excel-vba encoding

我已经实现了一个vba脚本,该脚本在每行的文件中创建文件夹,文件和说明。 问题是塞尔维亚拉丁字母,如“š,đ,č,ć,ž”。由于另一个程序,文件必须记录在ANSII .txt文件中。 更尴尬的是,第一个文件在.txt(ANSII编码)中是可以的,它保留了čćšđš字母,但其他文件都不正确..

也许我需要更改控制面板中的键盘设置?

在此链接上,我上传了测试文件:File

请运行这个简单的代码,请给我任何反馈。 谢谢!

Sub files()
Dim iRow As Long
Dim iFile As Integer
Dim sPath As String
Dim sFile As String


For iRow = 1 To Cells(Rows.Count, "C").End(xlUp).Row
    iFile = FreeFile
    With Rows(iRow)
        sPath = "E:\" & .Range("B1").Value & "\"
        If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath
        sFile = .Range("D1").Value & ".txt"
        Open sPath & sFile For Output As #iFile
        Print #iFile, .Range("E1").Value
        Close #iFile
    End With
Next iRow
End Sub

P.S。对于unicode中这些字符的列表,我希望使用ASCII:

  • U + 0106Ć0xC4 0x86 \ 304 \ 206Ć
  • U + 0107©0xC4 0x87 \ 304 \207ć
  • U +010CČ0xC40x8C \ 304 \214Č
  • U +010Dč0xC40x8D \ 304 \215č
  • U + 0110©0xC4 0x90 \ 304 \ 220©
  • U +0111đ0xC40x91 \ 304 \221đ
  • U +0160Š0xC50xA0 \ 305 \240Š
  • U +0161š0xC50xA1 \ 305 \241š
  • U +017DŽ0xC50xBD \ 305 \275Ž
  • U +017Ež0xC50xBE \ 305 \276ž

1 个答案:

答案 0 :(得分:1)

这是我尝试过的,它有效...

Sub files()
    Dim iRow As Long
    Dim sPath As String, sFile As String
    Dim fs As Object

    Set fs = CreateObject("Scripting.FileSystemObject")

    For iRow = 2 To Cells(Rows.Count, "C").End(xlUp).Row
        sPath = "E:\" & Range("B1").Value & "\"

        If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath

        sFile = Range("D" & iRow).Value & ".txt"

        With fs
            With .CreateTextFile(sPath & sFile, , True)
                .Write Range("E" & iRow).Value
                .Close
            End With
        End With
    Next iRow
End Sub

enter image description here