读/写文本文件,保留特殊章程但不更改BOM

问题描述 投票:0回答:1

看起来原始文件已经有 BOM,但我的 VBA 破坏了它。

使用 HEX 插件在 Notpad++ 中查看时,文本

""
或 HEX
"ef bb bf"
位于文件的开头。然后,在尝试使用 VBA 更改文件后,BOM 将成为记事本中可见文本的一部分,并阻止原始应用程序读取该文件。

原始文件编码是带 BOM 的 UTF-8,经过任何 VBA 编辑后,它仍然是带 BOM 的 UTF-8,但你在记事本文本中看到 BOM?

VBA 之后,文件以文本

""
和十六进制
"ef bb bf c3 af c2 bb c2 bf"
开始。

VBA无法匹配编码类型UTF-8和BOM?这可以在 VBA 中完成吗?

到目前为止,我可以使用流选项获得像 Ω 这样的特殊章程,但它会更改文件开头的 BOM,并导致稍后读取它的应用程序出现错误,或者使用第一个选项将 Ω 等章程更改为 O但不会改变BOM,原来的应用程序仍然可以读取它。

    ' Open the file for writing only if modifications were made
'    If fileContent <> modifiedContent Then
'        fileNumber = FreeFile
'        Open filePath For Output As #fileNumber
'        Print #fileNumber, modifiedContent
'        Close #fileNumber
'        MsgBox "The file has been successfully modified."
'    Else
'        MsgBox "No modifications were necessary."
'    End If

    ' Check if modifications were made
    If fileContent <> modifiedContent Then
        ' Create an instance of ADODB.Stream
        Set stream = CreateObject("ADODB.Stream")
        
        ' Specify the stream type (binary) and character set (UTF-8)
        stream.Type = 2 ' adTypeText
        stream.Charset = "utf-8"
        
        ' Open the stream and write the content
        stream.Open
        stream.WriteText modifiedContent
        
        ' Save the content to the file
        stream.SaveToFile filePath, 2 ' adSaveCreateOverWrite
        
        ' Close the stream
        stream.Close
        
        ' Clean up
        Set stream = Nothing
        
        MsgBox "The file has been successfully modified."
    Else
        MsgBox "No modifications were necessary."
    End If
excel vba file import
1个回答
0
投票

为了解决您的问题,您需要将输出视为二进制流。我修改了您的原始代码以实现此目的:

    ' Open the file for writing only if modifications were made
'    If fileContent <> modifiedContent Then
'        fileNumber = FreeFile
'        Open filePath For Output As #fileNumber
'        Print #fileNumber, modifiedContent
'        Close #fileNumber
'        MsgBox "The file has been successfully modified."
'    Else
'        MsgBox "No modifications were necessary."
'    End If

    ' Define the output byte array
    Dim outputContent() as Byte

    ' Check if modifications were made
    If fileContent <> modifiedContent Then

        ' Convert the modified text to a byte array
        outputContent = StrConv(modifiedcontent, vbFromUnicode)

        ' Create an instance of ADODB.Stream
        Set stream = CreateObject("ADODB.Stream")

        With stream

            ' Specify the stream type (binary) and character set (UTF-8)
            ' stream.Type = 2 ' adTypeText
            ' stream.Charset = "utf-8"          <--- not required
            .Type = 1 ' adTypeBinary

            ' Open the stream and write the content
            .Open
            'stream.WriteText modifiedContent
            .Write outputContent             '<--- output as binary

            ' Save the content to the file
            .SaveToFile filePath, 2 ' adSaveCreateOverWrite

            ' Close the stream
            .Close

        End With
        
        ' Clean up
        Set stream = Nothing
        
        MsgBox "The file has been successfully modified."
    Else
        MsgBox "No modifications were necessary."
    End If
© www.soinside.com 2019 - 2024. All rights reserved.