Er det muligt at konvertere en tekstfil med UTF-8 til UTF-8 uden BOM (fra en VBA makro i Excel)? Det virker selvfølgeligt hvis man går ind og bruger Notepad++, men ville gerne kunne gøre det automatisk.
Sub CSVFileAsUTF8WithoutBOM() Dim SrcRange As Range Dim CurrRow As Range Dim CurrCell As Range Dim CurrTextStr As String Dim ListSep As String Dim FName As Variant Dim UTFStream As Object Dim BinaryStream As Object
' ask for file name and path FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
'set field separator ListSep = "," 'set source range with data for csv file If Selection.Cells.Count > 1 Then Set SrcRange = Selection Else Set SrcRange = ActiveSheet.UsedRange End If
For Each CurrRow In SrcRange.Rows 'enclose each value with quotation marks and escape quotation marks in values CurrTextStr = "" For Each CurrCell In CurrRow.Cells CurrTextStr = CurrTextStr & """" & Replace(CurrCell.Value, """", """""") & """" & ListSep Next 'remove ListSep after the last value in line While Right(CurrTextStr, 1) = ListSep CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1) Wend 'add line to UTFStream UTFStream.WriteText CurrTextStr, adWriteLine Next
'skip BOM UTFStream.Position = 3
'copy UTFStream to BinaryStream Set BinaryStream = CreateObject("adodb.stream") BinaryStream.Type = adTypeBinary BinaryStream.Mode = adModeReadWrite BinaryStream.Open
'save to file BinaryStream.SaveToFile FName, adSaveCreateOverWrite BinaryStream.Flush BinaryStream.Close
End Sub
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.