用VBA将Unicode字符从TXT文件导入MS-WORD



我正在使用此代码将txt文件的行导入word:

Sub QuickType8()
    Dim strFilename As String
    Dim strTextLine As String
    Dim iFile As Integer: iFile = FreeFile
    Dim iLine As Integer
    On Error GoTo lbl_Exit
    strFilename = "C:UsersLongDropboxWIP word documents5) Common Phrases.txt"
    Open strFilename For Input As #iFile
    iLine = 0
    Do Until EOF(1)
        iLine = iLine + 1
        Line Input #1, strTextLine
        If iLine = 8 Then
            Exit Do
        End If
    Loop
    Close #iFile
    Selection.Text = strTextLine
lbl_Exit:
    Exit Sub
End Sub

但是,文本文件是用越南语编写的,因此其中有大量的Unicode字符。当数据被插入单词时,它被严重弄乱了(例如,"thờigian"变成了"•thýthýigian")。

我尝试使用Unicode编码保存文本文件,但它不起作用。

有什么办法可以解决这个问题?

以下代码似乎保留了格式。您需要设置" Microsoft脚本运行时"的引用。

Sub QuickType8()
Dim fso As New FileSystemObject
With fso
    Set strm = .OpenTextFile("C:PERSONALTempabc.txt", ForReading, False, TristateTrue)
    stex = Split(strm.ReadAll(), vbCrLf)
    For i = LBound(stex) To UBound(stex)
        If i = 8 Then Exit For
        strtx = strtx & vbCrLf & stex(i)
    Next
    Selection.Text = strtx
End With
End Sub

最新更新