0

I have the following VBA code that creates four files .csv csv.sha512 csv.sha256 and .csv.gz based on the excel table I have created.

Private Sub Export_Click()

Version = "1.0"
MyFileName = ActiveWorkbook.Name
MyFilePath = ActiveWorkbook.Path
MyFileName = Replace(MyFileName, ".xls", ".idl")
IdFiles = 1
fileName = MyFilePath & "\" & Cells(1, 3).Value & "_" & Format(Date, "yyyymmdd") & "_" & IdFiles & ".csv"

SelectionOnly = False
AppendData = False
delimeter = "|"
Application.ScreenUpdating = False
On Error GoTo EndMacro:


If SelectionOnly = True Then
    With Selection
        StartRow = 7
        StartCol = 2
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
Else
    With ActiveSheet.UsedRange
        StartRow = 7
        StartCol = 2
        EndRow = .Cells(.Cells.Count).Row
        EndCol = .Cells(.Cells.Count).Column
    End With
End If

CountRow = EndRow - StartRow + 1


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSOBlokada = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(fileName) Then
    IsExistCSV = True


    Do While IsExistCSV = True
       fileName = MyFilePath & "\" & Cells(1, 3).Value & "_" & Format(Date, "yyyymmdd") & "_" & IdFiles & ".csv"

       If Not objFSO.FileExists(fileName) Then
          IsExistCSV = False
       End If
       IdFiles = IdFiles + 1 
    Loop
End If

Set objFile = objFSO.OpenTextFile(fileName, 2, True, -1)
objFile.Writeline Version & delimeter & CountRow

    For RowNdx = StartRow To EndRow
        WholeLine = ""
        For ColNdx = StartCol To EndCol

            If Cells(RowNdx, ColNdx).Value = "" Then
                CellValue = ""
            Else
               CellValue = Cells(RowNdx, ColNdx).Value
            End If


            If ColNdx = EndCol Then
                WholeLine = WholeLine & CellValue
            Else
                WholeLine = WholeLine & CellValue & delimeter
            End If
        Next ColNdx
        WholeLine = Left(WholeLine, Len(WholeLine) - Len(sep))
        objFile.Writeline WholeLine
    Next RowNdx

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True

objFile.Close



Set objFileBlokada = objFSOBlokada.OpenTextFile(fileName & ".blokada", 2, True, -1)
objFileBlokada.Close


Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0

shellcmd1 = "cmd.exe /C certutil -hashfile """ & fileName & """ SHA512 | find /i /v ""sha512"" | find /i /v ""certutil"" > """ & fileName & ".sha512"""
wsh.Run shellcmd1, windowStyle, waitOnReturn


shacmd256 = "cmd.exe /C certutil -hashfile """ & fileName & """ SHA256 | find /i /v ""sha256"" | find /i /v ""certutil"" > """ & fileName & ".sha256"""


shellcmd2 = "cmd /C """"c:\Program Files\7-Zip\7z.exe"" a -w""" & MyFilePath & """ -tgzip """ & fileName & ".gz"" " & """" & fileName & """"""


End Sub

However files created are with BOM and with UCS2 encoding I need to have them with utf-8 encoding with LF as line end whereas now i have crlf as line end. How to fixt that?

Futhermore I need to have utf-8 without BOM which is quite difficult to achieve.

Any ideas how to even approach that?

mkc
  • 11
  • 5
  • Have you seen this https://stackoverflow.com/questions/2524703/save-text-file-utf-8-encoded-with-vba? – rohrl77 Jun 12 '19 at 08:46
  • Will generate with BOM futhermore I do not want to rewrite my macro in such a way to use `ADODB.Stream` – mkc Jun 12 '19 at 09:22

0 Answers0