Bonjour, je suis en train de créer une macro pour enregistrer un fichier CSV en UTF8.
J'ai trouvé un premier code, mais l'enregistrement se fait en UTF-8 avec BOM et la deuxième version que j'ai trouvé me redonne le fichier dans le même format que celui de départ. (Vérifié avec notepad++)
Voici le code
Function Choix_Fichier(titre As String) As String
With Application.FileDialog(msoFileDialogFilePicker)
'Définit un titre pour la boîte de dialogue
.Title = titre
'Affiche la boîte de dialogue
.Show
'Affiche le nom du dossier sélectionné
If .SelectedItems.Count > 0 Then
Choix_Fichier = .SelectedItems(1)
End If
End With
End Function
Sub convertUTF8()
Const adSaveCreateOverWrite = 2
Const adTypeBinary = 1
Const adTypeText = 2
Dim objStreamUTF8: Set objStreamUTF8 = CreateObject("ADODB.Stream")
Dim objStreamUTF8NoBOM: Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")
Dim tFileToOpen As String, tFileToSave As String
tFileToOpen = Choix_Fichier("Ouvrir")
tFileToSave = "testutf8.csv"
With objStreamUTF8
.Charset = "UTF-8"
.Type = adTypeText
.Open
.LoadFromFile tFileToOpen
.Position = 0
.SaveToFile "testutf8.csv", adSaveCreateOverWrite
.Type = adTypeBinary
.Position = 3
End With
With objStreamUTF8NoBOM
.Type = adTypeBinary
.Open
objStreamUTF8.CopyTo objStreamUTF8NoBOM
.SaveToFile "testutf8-2.csv", adSaveCreateOverWrite
End With
objStreamUTF8.Close
objStreamUTF8NoBOM.Close
End Sub
J'ai trouvé des codes similaire sur différents site mais aucun ne semble fonctionner.
La je tourne en rond... Quelqu'un aurait-il une idée ?
Par avance, merci