Saving a UTF-8 files without Byte Order Mark (BOM)

KenCriss

Active Member
Joined
Jun 6, 2005
Messages
326
Hi folks - I use the code below to save a regular text file as a UTF-8 file. The code works great...except the application that needs the file does not allow the BOM (byte order mark) to be at the beginning of the file. So, in the end I need a UTF-8 text file that does not have the BOM on the beginning of the file.

Options
1) Add code to rewrite the first line of the file taking out the BOM (3 bytes?) and then save the file again.
2) Change below code to save the file as UTF-8 without BOM (I am told you can do this in Notepad ++ with the File/SaveAs command but I don't know how to do that in VBA).
3) Other?

Code:
Sub SaveAsUTF8(sFileToSave As String)
     
    Dim fsT, tFileToOpen, tFileToSave As String
     
    tFileToOpen = sFileToSave
    tFileToSave = sFileToSave
     
    tFileToOpenPath = tFileToOpen
    tFileToSavePath = tFileToSave
     
Set fsT = CreateObject("ADODB.Stream"): 'Create Stream object
fsT.Type = 2: 'Specify stream type – we want To save text/string data.
fsT.Charset = "utf-8": 'Specify charset For the source text data.
     
fsT.Open: 'Open the stream
fsT.LoadFromFile tFileToOpenPath: 'And write the file to the object stream
     
fsT.SaveToFile tFileToSavePath, 2: 'Save the data to the named path
     
End Sub
Thanks for any help
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hello Mr KenCris,
I am also looking for a macro in VBA that save my excel sheet1 into a txt file ENCODED as: UTF-8 without BOM
I usually work with excel and have no much idea about macros in VBA, that's why I wonder if you have found any help or solution to your question.
Please I would be very gratefull if you could write the answer with the full code between Sub () and End Sub().

Let me know what libraries do I have to activate in excel.
Thanks Joe.
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,182
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top