Modifier encodage fichier txt ( UTF8 en ANSI ) en VBA

cs_Tricette Messages postés 2 Date d'inscription mardi 29 mai 2012 Statut Membre Dernière intervention 29 mai 2012 - 29 mai 2012 à 16:13
 rdstb - 4 août 2021 à 17:03
Bonjour,

je ne connais pas VB6. J'ai un code VBA qui appartient à une macro BO et je dois le faire évoluer.

Cette macro Bo exporte les données d'états BO dans un fichier texte. Ce fichier texte est encodé UTF-8.

J'ai besoin de modifier l'encodage de ce fichier texte pour de l'ANSI. j'ai adapté le script suivant mais ca plante sur la ligne en gras.

pourriez-vous m'aider, SVP?


Sub ModifEncodage(ByVal file_source As String, ByVal file_destination As String)

Const ForReading 1, ForWriting 2, ForAppending = 8
Const ModeAscii 0, ModeUnicode -1
Dim fso, f_in, f_out

Set fso = CreateObject("Scripting.FileSystemObject")

Set f_in = fso.openTextFile(file_source, ForReading, , ModeUnicode)
Set f_out = fso.openTextFile(file_destination, ForWriting, True, ModeAscii)
Dim Line() As Byte

Do Until f_in.AtEndOfStream
Line = StrConvf_in.readline,vbFromUnicode)
f_out.writeline Line
Loop
f_in.Close
f_out.Close

End Sub


ensuite, je n'ai pas encore codé mais il faut que je supprime f_in et renomme f_out en f_in.

merci d'avance.
A voir également:

5 réponses

MarcPL Messages postés 172 Date d'inscription jeudi 8 décembre 2011 Statut Membre Dernière intervention 21 juillet 2013 2
30 mai 2012 à 11:52
Le code d'ucfoutu est complet, il m'aurait bien servi il y a quelques temps ...

Pour un besoin de modifier des infos de fichiers PVR, j'ai créé ceci convenant bien aux caractères français :
Function Utxt(ByVal S As String) As String
    For N = 1 To Len(S)
         C = Mid(S, N, 1)
         A = Asc(C)
         If A > 169 Then C = "Ã" & Chr(A - 64)
         Utxt = Utxt & C
    Next
End Function


Function Wtxt(ByVal S As String) As String
    L = Len(S)

    For N = 1 To L
         C = Mid(S, N, 1)

         If C = "Ã" And N < L Then
             A = Asc(Mid(S, N + 1))

             If A > 105 And A < 192 Then
                C = Chr(A + 64)
                N = N + 1
             End If
         End If

         Wtxt = Wtxt & C
    Next
End Function

La fonction Utxt convertit un texte Windows, la fonction Wtxt convertit un texte UTF, si cela peut aider ...
___________________________________________________________________________________________________________________
Comme la vitesse de la lumière est supérieure à celle du son, certains ont l'air brillant avant d'avoir l'air con !
1
Bien pratique
0
Rejoignez-nous