从 vba 到平面文件的 Unicode 字符串

发布于 2024-07-24 14:45:49 字数 101 浏览 7 评论 0原文

我想将 excel/vba 宏中的 unicode 字符串存储在 Windows 盒子上的平面文件中。 该宏将普通字符串转换为 unicode 表示形式,需要将其存储在文件中并稍后检索。

I want to store a unicode string in a flat file on a windows box from an excel/vba macro. The macro converts normal string to unicode representation, need to store it in a file and retrieve later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏剧牡丹亭 2024-07-31 14:45:49

如前所述,您可以使用 Microsoft 脚本运行时 (scrrun.dll)。 我在下面发布了一些示例。 有些人还喜欢本机文件 IO 功能。 这里有一个广泛的(并且相当全面的线程)线程:http://www.xtremevbtalk。 com/showthread.php?t=123814

然而,对于 Unicode 文件,使用 Textstream 可能是最不痛苦的:)

Public Sub StringToTextFile(ByVal path As String, ByVal value As String)
    'Requires reference to scrrun.dll
    Dim fso As Scripting.FileSystemObject
    Dim ts As Scripting.TextStream
    Set fso = New Scripting.FileSystemObject
    Set ts = fso.CreateTextFile(path, False, True)
    ts.Write value
    ts.Close
End Sub

Public Sub LazyMansWay(ByVal path As String, ByVal value As String)
    'Reference counting will cause the objects to be destroyed. The termination
    'events of the classes will cause the connections to be closed.
    CreateObject("Scripting.FileSystemObject").CreateTextFile(path, False, True).Write value
End Sub

As mentioned, you can use the Microsoft Scripting Runtime (scrrun.dll). I have posted some examples below. Some people also like the native file IO features. There is an extensive (and fairly comprehensive thread) thread here: http://www.xtremevbtalk.com/showthread.php?t=123814

However for Unicode files it's probably the least painful to use Textstreams:)

Public Sub StringToTextFile(ByVal path As String, ByVal value As String)
    'Requires reference to scrrun.dll
    Dim fso As Scripting.FileSystemObject
    Dim ts As Scripting.TextStream
    Set fso = New Scripting.FileSystemObject
    Set ts = fso.CreateTextFile(path, False, True)
    ts.Write value
    ts.Close
End Sub

Public Sub LazyMansWay(ByVal path As String, ByVal value As String)
    'Reference counting will cause the objects to be destroyed. The termination
    'events of the classes will cause the connections to be closed.
    CreateObject("Scripting.FileSystemObject").CreateTextFile(path, False, True).Write value
End Sub
在梵高的星空下 2024-07-31 14:45:49

添加对“Microsoft Scripting Runtime”COM 组件 (scrrun.dll) 的引用。

它具有用于创建/读取/写入文件的所有类(特别是 FileSystemObject/TextStream)。

Add a reference to "Microsoft Scripting Runtime" COM component (scrrun.dll).

It has all the classes (specifically FileSystemObject/TextStream) to create/read/write files.

蓝咒 2024-07-31 14:45:49

我能想到的最好的解决方案是将字符串读入字节数组并将每个字节写入二进制文件

Private Function WriteBinaryFile(ByRef szData As String)
    Dim bytData() As Byte
    Dim lCount As Long

    bytData = szData
    Open PwdFileName For Binary As #1
        For lCount = LBound(bytData) To UBound(bytData)
            Put #1, , bytData(lCount)
        Next lCount
    Close #1
End Function

通过以二进制模式打开文件并将每个字节读入字节数组然后将其转换为字符串来读回它。

Sub ReadBinaryFile(ByRef gszData As String)
Dim aryBytes() As Byte
Dim bytInput As Byte
Dim intFileNumber
Dim intFilePos

intFileNumber = FreeFile

Open PwdFileName For Binary As #intFileNumber
intFilePos = 1

Do
    Get #intFileNumber, intFilePos, bytInput
    If EOF(intFileNumber) = True Then Exit Do
    ReDim Preserve aryBytes(intFilePos - 1)
    aryBytes(UBound(aryBytes)) = bytInput
    intFilePos = intFilePos + 1
Loop While EOF(intFileNumber) = False
Close #intFileNumber

gszData = aryBytes
End Sub

The best solution I could figure is read the string in to a byte array and write each byte to a binary file

Private Function WriteBinaryFile(ByRef szData As String)
    Dim bytData() As Byte
    Dim lCount As Long

    bytData = szData
    Open PwdFileName For Binary As #1
        For lCount = LBound(bytData) To UBound(bytData)
            Put #1, , bytData(lCount)
        Next lCount
    Close #1
End Function

Read it back by opening the file in binary mode and reading each byte into a byte array and then converting it to a string.

Sub ReadBinaryFile(ByRef gszData As String)
Dim aryBytes() As Byte
Dim bytInput As Byte
Dim intFileNumber
Dim intFilePos

intFileNumber = FreeFile

Open PwdFileName For Binary As #intFileNumber
intFilePos = 1

Do
    Get #intFileNumber, intFilePos, bytInput
    If EOF(intFileNumber) = True Then Exit Do
    ReDim Preserve aryBytes(intFilePos - 1)
    aryBytes(UBound(aryBytes)) = bytInput
    intFilePos = intFilePos + 1
Loop While EOF(intFileNumber) = False
Close #intFileNumber

gszData = aryBytes
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文