阅读VBA中的国际/特殊字符

时间:2015-03-25 13:17:22

标签: excel vba excel-vba

这是我第一次在excel中使用VBA来创建一个宏,它将逐行读取excel电子表格,并为每一行创建一个包含列中信息的文本文件。我的宏适用于普通文本,但有时在某些电子表格单元格中有外来文本。

一旦我的宏试图将其写入文本文件,它就会崩溃。第一个问题似乎是读取值(在调试器中查看外来文本显示为'?'问号),然后在尝试将这些问号写入文本文件时失败。

这是从一行读取值到字符串数组

的代码片段
    Set oFS = CreateObject("Scripting.Filesystemobject")
    For Each rID In oSh.UsedRange.Columns("A").Cells
        For Each rValue In oSh.UsedRange.Rows(rowCount).Cells
            ReDim Preserve columnValues(columnCount)
            columnValues(columnCount) = rValue
            columnCount = columnCount + 1
        Next
    Next

这是我写入文本文件的代码

sFNText = sMakeFolder & "\" & rID.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sFNText, 2, True)

For i = 0 To UBound(columnTitles)
     oTxt.Write columnTitles(i) & ": " & columnValues(i) & vbNewLine
Next i

oTxt.Close

我已尝试更改opentextfile的格式,并使用AscW和ChrW转换为ansi和来自ansi,但没有任何乐趣。请问有人可以解决我的问题,或者建议一个更简单的方法吗?

编辑:特别是我试图用希腊语符号(馅饼,欧米茄等)阅读并将它们写回文本文件。我用过

StrConv(Cells(1, 1), vbUnicode)

在类似的帖子中详细介绍的方法(请参阅注释),并且该示例正在运行。将此文本写入文本文件似乎是一个问题。 nixda的例子在使用他的打印命令时看起来效果很好,但是当我尝试使用时

otxt.Write

将我存储的变量写入文本文件,它写出垃圾 - 而不是产生正确结果的print方法。看一下调试器,两个变量的存储方式是相同的(print方法+ write),所以我相信它现在归结为输出方法(otxt.Write),它将存储的变量转换为垃圾。我试过使用-1& OpenTextFile的-2个选项 - 都产生垃圾结果。

3 个答案:

答案 0 :(得分:1)

我有以下表格:

enter image description here

和以下代码:

Sub writeUnicodeText()
 Dim arr_Strings() As String
 i = 0
 For Each oCell In ActiveSheet.Range("A1:A4")
  ReDim Preserve arr_Strings(i)
  arr_Strings(i) = oCell.Value
  i = i + 1
 Next

 Set oFS = CreateObject("Scripting.Filesystemobject")
 Set oTxt = oFS.OpenTextFile("C:\users\axel\documents\test.txt", 2, True, -1)
 For i = 0 To UBound(arr_Strings)
  oTxt.Write arr_Strings(i) & vbNewLine
 Next i
 oTxt.Close
End Sub

这将生成以下文件:

enter image description here

答案 1 :(得分:0)

这是我用来写入文本的代码。我已经尝试了很多方法,而且效果最好。

Sub ProcessX()
   FName1 = "Location of File"
   txtStrngX = OpenTextFileToString2(FName1)
end sub

Public Function OpenTextFileToString2(ByVal strFile As String) As String

 Dim hFile As Long
 hFile = FreeFile
 Open strFile For Input As #hFile
 OpenTextFileToString2 = Input$(LOF(hFile), hFile)
 Close #hFile

End Function

至于从行读入时,请确保在编译时将变量设置为字符串,并且任何方法都可以正常工作。

答案 2 :(得分:0)

对不起。那是从文本中读取的。这是写作。

Public Function RecordsetToText(rs As Object, Optional FullPath _
    As String, Optional ValueDelimiter As String = "  ") As Boolean

 'PURPOSE: EXPORTS DATA FROM AN ADO RECORDSET TO A TEXT FILE


 'PARAMETERS:


 'RS: Recordset to Export. Open the recordset before

 'passing it to this function


 'FullPath (Optional): FullPath of text file.

 'if not specified, the function uses app.path +

 'rs.txt


 'ValueDelmiter (Optional): String to delimiter

 'values within a row.  If not specified, an tab

 'is used


 'RETURNS: True if successful, false if an error occurs


 'COMMENTS: Rows are delimited by a carriage return

 Dim sFullPath As String

 Dim sDelimiter As String

 Dim iFileNum As Integer

 Dim lFieldCount As Long

 Dim lCtr As Long

 Dim oField As ADODB.Field


 On Error GoTo ErrorHandler:



 If RecordSetReady(rs) = False Then Exit Function



 sDelimiter = ValueDelimiter


 If FullPath = "" Then

     sFullPath = App.Path

     If Right(sFullPath, 1) <> "\" Then sFullPath = _

        sFullPath & "\"


     sFullPath = sFullPath & "rs.txt"

 Else

     sFullPath = FullPath

 End If


 iFileNum = FreeFile


 Open sFullPath For Output As #iFileNum


 With rs

 lFieldCount = .Fields.Count - 1


 On Error Resume Next

 .MoveFirst

 On Error GoTo ErrorHandler


 For lCtr = 0 To lFieldCount

    Set oField = .Fields(lCtr)

     If lCtr < lFieldCount Then

         Print #iFileNum, oField.Name & sDelimiter;

    Else

        Print #iFileNum, oField.Name

    End If

 Next


 Do While Not .EOF

    For lCtr = 0 To lFieldCount

    Set oField = .Fields(lCtr)

      If lCtr < lFieldCount Then

          Print #iFileNum, oField.Value & sDelimiter;

     Else

          Print #iFileNum, oField.Value

       End If

  Next

 .MoveNext

 Loop


 End With


 RecordsetToText = True


 ErrorHandler:

  On Error Resume Next

  Close #iFileNum


 End Function
相关问题