本文介绍了VBA:用没有BOM的UTF-8保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是简单的,这是我试过的:

 设置objStream = CreateObject(ADODB.Stream)
设置objStreamNoBOM = CreateObject(ADODB.Stream)

带objStream
.Open
.Charset =UTF-8
.WriteTextaaaaaa
.Position = 0
结束

带objStreamNoBOM
'.Charset =Windows-1252'工作
.Charset =UTF- 8不工作!
.Open
.Type = 2
.WriteText objStream.ReadText
.SaveToFiletoto.php,2
.Close
结束
objStream.Close

如果字符集是UTF-8,那么在开始处有ï»对于如何使用UTF-8和没有BOM保存文件的任何想法?

解决方案

在所有可能的世界中,相关列表将包含对的第二个策略:

  Option Explicit 

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

Dim objStreamUTF8:设置objStreamUTF8 = CreateObject(ADODB.Stream)
Dim objStreamUTF8NoBOM:设置objStreamUTF8NoBOM = CreateObject(ADODB.Stream)

With objStreamUTF8
.Charset =UTF-8
.Open
.WriteTextaÄö
.Position = 0
.SaveToFiletoto.php ,adSaveCreateOverWrite
.Type = adTypeBinary
.Position = 3
结束

带objStreamUTF8NoBOM
.Type = adTypeBinary
.Open
objStreamUTF8.CopyTo objStreamUTF8NoBOM
.SaveToFiletoto-nobom.php,adSaveCreateOverWrite
End with

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

证据:

  chcp 
活动代码页:65001

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

类型toto-nobom.php
aÄö


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

 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

if the charset is UTF-8, then there is ï» at the beginning of the file.

Any idea on how to save a file with UTF-8 and without BOM?

解决方案

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     = adTypeBinary
  .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Äö

这篇关于VBA:用没有BOM的UTF-8保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:36