programing

VBA : BOM 없이 UTF-8로 파일 저장

subpage 2023. 7. 3. 22:53
반응형

VBA : BOM 없이 UTF-8로 파일 저장

그것은 아마도 단순할 것입니다. 제가 시도한 것은 다음과 같습니다.

 Set objStream = CreateObject("ADODB.Stream")
 Set objStreamNoBOM = CreateObject("ADODB.Stream")

 With objStream
        .Open
        .Charset = "UTF-8"
        .WriteText "aaaaaa"
        .Position = 0
    End With

    With objStreamNoBOM
      '.Charset = "Windows-1252"   ' WORK
       .Charset = "UTF-8"          ' DOESN'T WORK!!
       .Open
       .Type = 2
       .WriteText objStream.ReadText
       .SaveToFile "toto.php", 2
       .Close
    End With
    objStream.Close

문자 집합이 UTF-8이면 파일의 시작 부분에 »가 있습니다.

UTF-8과 BOM을 사용하지 않고 파일을 저장하는 방법에 대해 알고 계십니까?

가능한 모든 세계 중에서 관련 목록은 "vbscript adodb.stream bom vbscript site:stackoverflow.com "의 첫 번째 히트로 찾은 이 질문에 대한 참조를 포함할 것입니다.

Boost의 답변에서 나온 두 번째 전략을 기반으로 합니다.

Option Explicit

Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText   = 2

Dim objStreamUTF8      : Set objStreamUTF8      = CreateObject("ADODB.Stream")
Dim objStreamUTF8NoBOM : Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

With objStreamUTF8
  .Charset = "UTF-8"
  .Open
  .WriteText "aÄö"
  .Position = 0
  .SaveToFile "toto.php", adSaveCreateOverWrite
  .Type     = adTypeText
  .Position = 3
End With

With objStreamUTF8NoBOM
  .Type    = adTypeBinary
  .Open
  objStreamUTF8.CopyTo objStreamUTF8NoBOM
  .SaveToFile "toto-nobom.php", adSaveCreateOverWrite
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

증거:

chcp
Active code page: 65001

dir
 ...
15.07.2015  18:48                 5 toto-nobom.php
15.07.2015  18:48                 8 toto.php

type toto-nobom.php
aÄö

스크립팅 파일 시스템 개체의 스트림에 바이트 순서 표시가 삽입된 것은 알고 있었지만 ADODB 스트림에서는 확인할 수 없었습니다.

아니면 적어도 아직은 아닙니다. 저는 ADODB 스트림 객체를 거의 사용하지 않습니다.

하지만 몇 년 전에 이 말을 암호화했던 것을 기억합니다.

'   ****   WHY THIS IS COMMENTED OUT   **** **** **** **** **** **** **** ****
'
'   Microsoft ODBC and OLEDB database drivers cannot read the field names from
'   the header when a unicode byte order mark (&HFF & &HFE) is inserted at the
'   start of the text by Scripting.FileSystemObject 'Write' methods. Trying to
'   work around this by writing byte arrays will fail; FSO 'Write' detects the
'   string encoding automatically, and won't let you hack around it by writing
'   the header as UTF-8 (or 'Narrow' string) and appending the rest as unicode
'
'   (Yes, I tried some revolting hacks to get around it: don't *ever* do that)
'
'   **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
'
'    With FSO.OpenTextFile(FilePath, ForWriting, True, TristateTrue)
'        .Write Join(arrTemp1, EOROW)
'        .Close
'    End With ' textstream object from objFSO.OpenTextFile
'
'   **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****

당신은 제가 나쁜 하루를 보냈다는 것을 알 수 있습니다.

다음으로 파일 처리 이전 시대의 PUT 명령어를 사용하여 원시 C:

'   **** WHY WE 'PUT' A BYTE ARRAY INSTEAD OF A VBA STRING VARIABLE  **** ****
'
'       Put #hndFile, , StrConv(Join(arrTemp1, EOROW), vbUnicode)
'       Put #hndFile, , Join(arrTemp1, EOROW)
'
'   If you pass unicode, Wide or UTF-16 string variables to PUT, it prepends a
'   Unicode Byte Order Mark to the data which, when written to your file, will
'   render the field names illegible to Microsoft's JET ODBC and ACE-OLEDB SQL
'   drivers (which can actually read unicode field names, if the helpful label
'   isn't in the way). However, the 'PUT' statements writes a Byte array as-is
'
'   **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****

그래서 실제로 그것을 수행하는 코드가 있습니다.

Dim arrByte() As Byte
Dim strText   As String
Dim hndFile   As String


    strText = "Y'all knew that strings are actually byte arrays?"
    arrByte = strText 

    hndFile = FreeFile
    Open FilePath For Binary As #hndFile

    Put #hndFile, , arrByte
    Close #hndFile

    Erase arrByte

저는 strText가 실제로 UTF-8이라고 가정합니다. 제 말은, 우리는 마이크로소프트 오피스에 있는 VBA에 있고, 우리는 이것이 항상 UTF-8이 될 것이라는 것을 알고 있습니다. 심지어 우리가 외국에서 사용하는 것일지라도...

...그렇죠?

언급URL : https://stackoverflow.com/questions/31435662/vba-save-a-file-with-utf-8-without-bom

반응형