Forum Discussion

FabriceB's avatar
FabriceB
Occasional Contributor
8 years ago

Nead to write utf8 Without BOM

Hi,

 

I need to read / Write into a textfile on utf-8 but without bom. Actually, if a use Writetotextfile with textencodingType 22 or aqfile.ctUTF8, my file have a BOM... when my application read this BOM... it BOOOOOM.

 

it is possible to read/write without this Bom ?

 

Thx

 

Fabrice

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Hi Fabrice,

     

    Both aqFile* and Scripting.FileSystemObject write UTF-8 files with BOM.

     

    To write a file without BOM, you can use this workaround that involves two ADO Stream objects:

    http://stackoverflow.com/questions/31435662/vba-save-a-file-with-utf-8-without-bom/31436631#31436631

    The example is in VBScript, but the code can be ported to other languages.

     

    Another way is to use the .NET method System.IO.File.WriteAllText(), which by default uses UTF-8 encoding without BOM. You will need to add the System.IO assembly to project properties > CLR Bridge.

    dotNET.System_IO.File.WriteAllText("C:\\no_bom.txt", "привет");
    • FabriceB's avatar
      FabriceB
      Occasional Contributor

      Hi all,

       

      Thanks for your responses...

       

      dotNET.System_IO.File.WriteAllText("C:\\no_bom.txt", "привет");

      It works fine, thanks again

       

      Fabrice

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Rather than using the aqFile.OpenTextFile, etc., you could instantiate the FileSystemObject yourself an use it's native functions (aqFile and aqFileSystem provide wrappers around those methods).