Save csv file as UTF-8 without BOM in VBA

bukimi

Board Regular
Joined
Apr 12, 2017
Messages
105
Office Version
  1. 2019
Platform
  1. Windows
Hello!

I tried different codes from different sites, but none of the solutions seems to work fine. I have a macro that cuts big data into smaller chunks and then saves them into seperate csv files, that are then manually sent to some server to read that automatically.
To be read properly a file needs to meet specific criteria: UTF8 coding, no BOM, without quotation marks as text qualifier.
My Excel is 2019.

The (almost) working piece of "save file" code is as follows:
VBA Code:
Private Sub SaveFilesForServer()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim WB As Workbook
Set WB = ActiveWorkbook

ActiveWorkbook.WebOptions.Encoding = msoEncodingUTF8

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName
'CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "S:\AnaliticsFiles\"

For i = 1 To WB.Sheets.Count
WB.Sheets(i).Activate
    WB.Sheets(i).SaveAs SaveToDirectory & WB.Sheets(i).Name, FileFormat:=xlCSVUTF8

Next

End Sub

Opening created files with Notepad+ shows 2 problems:
1. Some lines in some files start and end with Quotation marks (there are none in source data in Excel). I didn't figure out what determines which lines are influenced. Most possibly something goes wrong with Text Qualifier. It happens only when saving by VBA. Saving manually with "save as -> UTF-8" doesn't produce these wild quotation marks.
2. All files contain BOM. I need to manually convert all files with another program to get rid of it.
Is there any way to solve problem 1 and/or 2 with VBA? Now I get 2 extra steps for all my work only for converting it :(
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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