首页 > 解决方案 > VBA 将 UTF-8 字符附加到文本文件而不获取“???”

问题描述

您好,我在 MS Access VBA 中工作,我正在编写代码以遍历所有查询定义并将它们附加到文本文件中。当查询 sql 包含 utf-8 字符时,我的输出文件包含“???” 应该出现 UTF-8 字符的位置。我希望 utf-8 字符出现在文本文件中。我的代码如下,任何帮助将不胜感激。

Sub PrintQueries()
Dim strSeparator    As String       
Dim strFileName     As String       
Dim returnvalue     As Variant      
Dim qdef            As QueryDef
Dim qdefs           As QueryDefs
Dim strQueryName    As String       
Dim strQuery        As String       
Dim strdate         As String       

    
    strdate = Replace(Replace(Now(), "/", "-"), ":", ".")
    Set qdefs = Application.CodeDb.QueryDefs
    
    strFileName = "C:\temp\Queries_" & strdate & ".txt"
    For Each qdef In qdefs
        strQueryName = qdef.Name
        If Left(strQueryName, 1) <> "~" Then
            Call TxtAppend(strFileName, strQueryName)
            strQuery = qdef.SQL                          '<-- What if strQuery contains utf-8?
            Call TxtAppend(strFileName, strQuery)        '<-- Call function
            strSeparator = "===================="
            Call TxtAppend(strFileName, strSeparator)
        End If
    Next
    returnvalue = Shell("notepad.exe" & strFileName, vbNormalFocus)
End Sub

Public Function TxtAppend(strFileName As String, strText As String)

Dim intFileNumber   As Integer 
   
    intFileNumber = FreeFile

    Open strFileName For Append As #intFileNumber

    Print #intFileNumber, strText

    Close #intFileNumber

End Function

标签: vbams-accessutf-8

解决方案


推荐阅读