IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Modifier fichier .txt avec une macro


Sujet :

Macros et VBA Excel

  1. #21
    Futur Membre du Club
    Homme Profil pro
    Stagiaire
    Inscrit en
    Août 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Stagiaire
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2013
    Messages : 22
    Points : 7
    Points
    7
    Par défaut
    Le fichier texte "brut" sur lequel je veux effectuer mes modifs est un fichier généré par Dymola. Et il semblerait que les fichiers de Modelica / Dymola soient encodés en UTF-8.

  2. #22
    Invité
    Invité(e)
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    Sub Test()
    Do While Not EOF(1)
        Line Input #1, TextLine 'Lecture de la ligne
               txt = txt & Decode_UTF8(TextLine) & vbCrLf 'Je concatène mon texte
        Loop
     
     Montable = Split(txt, vbCrLf)
     Open Filename For Output As #1 'Ouverture du fichier en écriture.
     For i = 1 To UBound(Montable) - 1 'Ne prend pas en compte la ligne 0 et la ligne UBound
        Print #1, Encode_UTF8(Trim("" & Montable(i)))  'Je stock dans mon fichier la variable Montable(I)
    Next
    Close #1
    End Sub
    Public Function isUTF8(astr)
        Dim c0, c1, c2, c3
        Dim n
     
        isUTF8 = True
        n = 1
        Do While n <= Len(astr)
            c0 = Asc(Mid(astr, n, 1))
            If n <= Len(astr) - 1 Then
                c1 = Asc(Mid(astr, n + 1, 1))
            Else
                c1 = 0
            End If
            If n <= Len(astr) - 2 Then
                c2 = Asc(Mid(astr, n + 2, 1))
            Else
                c2 = 0
            End If
            If n <= Len(astr) - 3 Then
                c3 = Asc(Mid(astr, n + 3, 1))
            Else
                c3 = 0
            End If
     
            If (c0 And 240) = 240 Then
                If (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
                    n = n + 4
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 224) = 224 Then
                If (c1 And 128) = 128 And (c2 And 128) = 128 Then
                    n = n + 3
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 192) = 192 Then
                If (c1 And 128) = 128 Then
                    n = n + 2
                Else
                    isUTF8 = False
                    Exit Function
                End If
            ElseIf (c0 And 128) = 0 Then
                n = n + 1
            Else
                isUTF8 = False
                Exit Function
            End If
        Loop
    End Function
    Public Function Encode_UTF8(astr)
        Dim c
        Dim n
        Dim utftext
     
        utftext = ""
        n = 1
        Do While n <= Len(astr)
            c = AscW(Mid(astr, n, 1))
            If c < 128 Then
                utftext = utftext + Chr(c)
            ElseIf ((c >= 128) And (c < 2048)) Then
                utftext = utftext + Chr(((c \ 64) Or 192))
                utftext = utftext + Chr(((c And 63) Or 128))
            ElseIf ((c >= 2048) And (c < 65536)) Then
                utftext = utftext + Chr(((c \ 4096) Or 224))
                utftext = utftext + Chr((((c \ 64) And 63) Or 128))
                utftext = utftext + Chr(((c And 63) Or 128))
            Else ' c >= 65536
                utftext = utftext + Chr(((c \ 262144) Or 240))
                utftext = utftext + Chr(((((c \ 4096) And 63)) Or 128))
                utftext = utftext + Chr((((c \ 64) And 63) Or 128))
                utftext = utftext + Chr(((c And 63) Or 128))
            End If
            n = n + 1
        Loop
        Encode_UTF8 = utftext
    End Function
    Public Function Decode_UTF8(astr)
        Dim c0, c1, c2, c3
        Dim n
        Dim unitext
     
        If isUTF8(astr) = False Then
            Decode_UTF8 = astr
            Exit Function
        End If
     
        unitext = ""
        n = 1
        Do While n <= Len(astr)
            c0 = Asc(Mid(astr, n, 1))
            If n <= Len(astr) - 1 Then
                c1 = Asc(Mid(astr, n + 1, 1))
            Else
                c1 = 0
            End If
            If n <= Len(astr) - 2 Then
                c2 = Asc(Mid(astr, n + 2, 1))
            Else
                c2 = 0
            End If
            If n <= Len(astr) - 3 Then
                c3 = Asc(Mid(astr, n + 3, 1))
            Else
                c3 = 0
            End If
     
            If (c0 And 240) = 240 And (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 240) * 65536 + (c1 - 128) * 4096) + (c2 - 128) * 64 + (c3 - 128)
                n = n + 4
            ElseIf (c0 And 224) = 224 And (c1 And 128) = 128 And (c2 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 224) * 4096 + (c1 - 128) * 64 + (c2 - 128))
                n = n + 3
            ElseIf (c0 And 192) = 192 And (c1 And 128) = 128 Then
                unitext = unitext + ChrW((c0 - 192) * 64 + (c1 - 128))
                n = n + 2
            ElseIf (c0 And 128) = 128 Then
                unitext = unitext + ChrW(c0 And 127)
                n = n + 1
            Else ' c0 < 128
                unitext = unitext + ChrW(c0)
                n = n + 1
            End If
        Loop
     
        Decode_UTF8 = unitext
    End Function

  3. #23
    Futur Membre du Club
    Homme Profil pro
    Stagiaire
    Inscrit en
    Août 2013
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Stagiaire
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2013
    Messages : 22
    Points : 7
    Points
    7
    Par défaut
    Merci bien, mais je ne suis pas tout à fait sûr de comprendre le fonctionnement exact de ce code.
    Il y a une fonction encodage, une fonction décodage (je ne comprends pas trop d'où viennent tous les nombres utilisés dans ces fonctions mais soit) et je dois donc mettre le coeur de la macro (remplacement de certaines données) entre le décodage et l'encodage ?

  4. #24
    Invité
    Invité(e)
    Par défaut
    la fonction Decode_UTF8, convertie tes données du format UTF8 au Format ISSO.

    la fonction Encode_UTF8 convertie le format ISO en UTF8.

    ici on test le code ASCII étendu le code ASCII de "é" est 233 alors que le code ASCII en UTF8 est 195.

    c'est pour cela que tu voie que des chiffres car on traite le code ASCII.

    Pour t'en convaincre, dans la fenêtre d'exécution touches [Ctrl] + [g],copie col ce texte :
    Code fenêtre d'exécution : Sélectionner tout - Visualiser dans une fenêtre à part
    ?"ISO : é UTF : " & Encode_UTF8("é")& " Decode_UTF8 : " & Decode_UTF8(Encode_UTF8("é"))
    puis [Entre] et regarde le résultat!
    Dernière modification par AlainTech ; 25/09/2013 à 06h51. Motif: Fusion de 2 messages

Discussions similaires

  1. Réponses: 2
    Dernier message: 01/05/2007, 18h50
  2. Réponses: 3
    Dernier message: 05/03/2007, 23h54
  3. modifier fichier .txt avec Visual Basic
    Par marco1980 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 29/09/2006, 20h19
  4. Réponses: 3
    Dernier message: 23/09/2006, 15h19
  5. [VBA-E] Exporter un fichier Web avec une macro
    Par Wilgard dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 22/05/2006, 12h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo