View Full Version : Strange character (format) when merging 2 utf-8 files

07-20-2017, 11:13 PM
I have a VBA Excel macro that loads content of 2 utf-8 files and add/save it to another utf-8 file. Part of the code is:

Set oResult = CreateObject("ADODB.Stream")
oResult.Type = 1
AddFileToStream oResult, ReadBinaryFile("c:\path\file1.csv")
AddFileToStream oResult, ReadBinaryFile("c:\path\file2.csv")

' save the file
oResult.SaveToFile ("c:\merge\file.csv")
Next i
End Sub

Sub AddFileToStream(ByRef oStream, ByVal btBinary)
oStream.Position = oStream.Size
oStream.Write (btBinary)
End Sub

Function ReadBinaryFile(ByVal sFilePath)
Dim oStream
Set oStream = CreateObject("ADODB.Stream")
oStream.Type = 1
oStream.LoadFromFile sFilePath
ReadBinaryFile = oStream.Read
Set oStream = Nothing
End Function

Problem is that, when macro merged them, the first data entry of second merging document in the merged document (utf-8 file) have these strange format characters and I cannot find the way to get rid of them. The outcome file has to be without these characters because other system doesn't work and procedure stops with error. Probably the reason why it is hapenning is because it's chucking in the file header which specifies that the file is in UTF-8 format. It loos like this:

Is it possible to solve it?

07-20-2017, 11:30 PM
Try change .Type=1 in both lines to .Type=2 ...
Can you upload a sample of your workbook?

07-21-2017, 01:26 AM
This should be sufficient:

sub M_snb
shell "cmd /c copy c:\path\*.csv C:\integrated.csv"
end sub

Now all csv files in the direcory have been merged into 1 file : integrated.csv.

07-21-2017, 04:42 AM
It helps, but I need to change ReadBinaryFile = oStream.Read to ReadBinaryFile = oStream.ReadText otherwise error occured. Same with oStream.Write (btBinary) (oStream.WriteText (btBinary)).