Consulting

Results 1 to 4 of 4

Thread: Strange character (format) when merging 2 utf-8 files

  1. #1
    VBAX Newbie
    Joined
    Jul 2017
    Posts
    3
    Location

    Strange character (format) when merging 2 utf-8 files

    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:

    Sub
    Set oResult = CreateObject("ADODB.Stream")
    oResult.Type = 1
    oResult.Open

    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.Open
    oStream.LoadFromFile sFilePath
    ReadBinaryFile = oStream.Read
    oStream.Close
    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:
    CSV_problem.jpg

    Is it possible to solve it?

  2. #2
    Hello
    Try change .Type=1 in both lines to .Type=2 ...
    Can you upload a sample of your workbook?

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    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.
    Last edited by snb; 07-21-2017 at 05:24 AM.

  4. #4
    VBAX Newbie
    Joined
    Jul 2017
    Posts
    3
    Location
    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)).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •