VBA : save a file with UTF-8 without BOM

前端 未结 2 1357
南旧
南旧 2020-11-29 07:11

it\'s probably sthg simple, here is what I tried :

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

 Wit         


        
2条回答
  •  被撕碎了的回忆
    2020-11-29 07:22

    In the best of all possible worlds the Related list would contain a reference to this question which I found as the first hit for "vbscript adodb.stream bom vbscript site:stackoverflow.com".

    Based on the second strategy from boost's answer:

    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
    

    Evidence:

    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Äö
    

提交回复
热议问题