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 :

Problème d'import de caractères chinois


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2017
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Mayenne (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2017
    Messages : 8
    Points : 3
    Points
    3
    Par défaut Problème d'import de caractères chinois
    Bonjour, je suis débutant en vba, et il m'a été confia une tâche, elle consiste à importer des fichiers .properties dans un excel.
    J'ai réussi jusqu'au moment ou un fichier contient des caractères chinois...
    Le fichier est codé en UTF8, et quand je l'importe sous excel tous les caractères non chinois passent, mais les chinois ne passent pas, ils sont mal écrits.
    Voici un exemple : Le mot "序列" devient "åºåˆ—"
    J'ai cherché sur internet mais je ne trouve pas de solution, en espérant qu'une âme charitable me vienne à la rescousse.
    Merci d'avance !

  2. #2
    Expert éminent sénior

    Profil pro
    Conseil, Formation, Développement - Indépendant
    Inscrit en
    Février 2010
    Messages
    8 420
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Conseil, Formation, Développement - Indépendant

    Informations forums :
    Inscription : Février 2010
    Messages : 8 420
    Points : 16 264
    Points
    16 264
    Par défaut
    Bonjour

    On peut importer de l'UTF8 dans les onglets Excel, surtout 2013.

    Comment se fait l'import ?

    Par contre je ne penses pas que cela puisse s'importer dans le module VBA.
    Chris
    PowerQuery existe depuis plus de 13 ans, est totalement intégré à Excel 2016 &+. Utilisez-le !

    Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson.
    Confucius

    ----------------------------------------------------------------------------------------------
    En cas de résolution, n'hésitez pas cliquer sur c'est toujours apprécié...

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2017
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Mayenne (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2017
    Messages : 8
    Points : 3
    Points
    3
    Par défaut
    Bonjour, merci de ta réponse. L'import se fait via une macro que j'ai créée moi même. Voici le code

    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
     
    Sub import_real()
     
    Application.ScreenUpdating = False 'the screen is not updating during the function
    On Error GoTo ErrorCode 'if ther is an error, the programme execute the code in "ErrorCode" below
    Dim FileIndex As Integer 'FileIndex is an Integer
    Dim LineContent As String 'LineContent is a String
    Dim I As Integer 'I is an Integer
    Dim J As Integer 'J is an Integer
    Dim table() As String 'table1() is a table of String
    Dim part_a As String 'part_a is a String
    Dim part_b As String 'part_b is a String
    Dim switch As String 'switch is a String
    Dim char As String 'char is a String
    Dim color
    Dim group As String 'group is a String
    Dim Case_Id As String 'Case_Id is a String
    Dim nb As Integer 'nb is an Integer
    Dim name As String 'name is a String
    Dim A As String 'A is a String
    Dim B As String 'B is a String
    Dim C As String 'C is a String
    FileIndex = FreeFile()
    Sheets("Parameters").Activate 'Activate the "Parameters" sheet on the excel doc
    nb = Application.CountA([A:A]) 'Count the number of filled case in the collumn A
    For J = 2 To nb 'Execute a loop for each filled case in the collumn A
        A = "A" & J
        B = "B" & J
        C = "C" & J
        name = Sheets("Parameters").Range(A).Value 'name take the value of the case A corresponding to the value of J
        I = 2
        Open name For Input As #FileIndex 'open the file corresponding to "name"
        group = Sheets("Parameters").Range(C).Value 'group take the value of the case C corresponding to the value of J
        Case_Id = "J" & Application.Match(group, Sheets("Parameters").Range("J:J"), 0) 'Find the case in the collumn J where there is the word corresponding to "group"
        color = Sheets("Parameters").Range(Case_Id).Interior.color 'color take the value of the "Case_Id" color
        Sheets.Add After:=ActiveSheet 'open a new sheet in Excel
        ActiveSheet.name = Sheets("Parameters").Range(B).Value 'rename the sheet with the name of the file
        ActiveSheet.Tab.color = color 'color the sheet with "color"
        Call design 'call design funtion below
        While Not EOF(FileIndex) 'While there is still a line in the file execute the code below
            Line Input #FileIndex, LineContent     ' read the file line by line: LineContent take the content of the active line
            cells(I, 1).Value = LineContent 'the case (i,1) in the excel doc take the value of LineContent
            switch = cells(I, 1).Value 'switch take the value of the cells(I,1)
            If Mid(switch, 1, 3) = "" Then 'If there is the 3 characteres at the start
                cells(I, 1).Value = Replace(switch, Left(switch, 3), "") 'delete it
            End If
            switch = cells(I, 1).Value 'switch take the value of cells(i,1)
            char = Left(switch, 1) 'char take the first charactere of LineContent
            If char <> "#" Then
                table = split(LineContent, "=") 'we cut the line in to part when ther is an "=" in the line and we place the to part of the line in the table
                part_a = table(0) 'part_a take the value of the first case of the table (the first part of the line)
                part_b = table(1) 'part_b take the value of the second case of the table (the second part of the line)
                cells(I, 1).Value = part_a 'the case (i,1) in the excel doc take the value of part_a
            cells(I, 2).Value = part_b   'the case (i,2) in the excel doc take the value of part_b
            End If
            I = I + 1
        Wend
        Call replace_char
        Close #FileIndex 'close the file wich is open
    Next
    Sheets("Action").Activate 'open the "Action" sheet
    Application.ScreenUpdating = True 'update the screen
    MsgBox "Import done" 'display a message if the import was done
    Exit Sub
     
    ErrorCode: 'the code below is executing if there is a probleme during the function
    MsgBox "An error has occured..." 'Display an error message
    Application.ScreenUpdating = True 'update the screen
    End Sub
     
    Sub design()
    Range("A1").Value = "Key Word" 'The case "A1" take the value "Key Word"
    Range("A1").Font.Italic = True 'apply italic on the case "A1"
    Range("A1").Font.Bold = True 'apply bold on the case "A1"
    Range("B1").Value = "Meaning" 'The case "B1" take the value "Meaning"
    Range("B1").Font.Italic = True 'apply italic on the case "B1"
    Range("B1").Font.Bold = True 'apply bold on the case "B1"
    Columns("A:A").ColumnWidth = 56.57 'Change the width of the column A
    Columns("B:B").ColumnWidth = 48.29 'Change the width of the column B
    End Sub
     
    Sub replace_char()
    ReDim A_Remplacer(0 To 32)
    ReDim Remplacants(0 To 32)
    Dim I As Byte
    A_Remplacer = Array("ß", "ï", "ó", "Î", "ä", "ü", "ö", "î", "é", "→", "Ü", "ù", "â", "è", "à ", "’", "€", "®", "Ø", "°", "ç", "ô", _
                      "«", "»", "û", "ê", "Ã*", "ú", "á", "ñ", "¿", "Ú", "Í")
    Remplacants = Array("ß", "ï", "ó", "Î", "ä", "ü", "ö", "î", "é", Chr(26), "Ü", "û", "â", "è", "à", "'", "€", "®", "Ø", "°", "ç", "ô", _
                      Chr(34), Chr(34), "û", "ê", "í", "ú", "á", "ñ", "¿", "Ú", "Í")
    For I = 0 To 32
        cells.Replace What:=A_Remplacer(I), Replacement:=Remplacants(I), LookAt:=xlPart
    Next I
    End Sub
    --------------------------------------
    La sub replace_char je l'utilise pour remettre les accents et autres caractères modifiés lors de l'import...

  4. #4
    Expert éminent sénior Avatar de Menhir
    Homme Profil pro
    Ingénieur
    Inscrit en
    Juin 2007
    Messages
    16 037
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2007
    Messages : 16 037
    Points : 32 866
    Points
    32 866
    Par défaut
    Il est probable que ce soit simplement un problème de police de caractère.

    Regarde quelle est la police des textes écrits en chinois, essaye de l'obtenir et de l'installer sur ton poste.
    Il est probable que ce problème d'affichage disparaitra.

    Pour le vérifier, essaye de passer l'un des ces textes chinois en police "Microsoft YaHei UI". Les caractères devraient s'afficher correctement (à moins que ta macro replace_char ne mettre le brin là-dedans).
    Merci de cliquer sur pour chaque message ayant aidé puis sur pour clore cette discussion.

  5. #5
    Expert éminent sénior

    Profil pro
    Conseil, Formation, Développement - Indépendant
    Inscrit en
    Février 2010
    Messages
    8 420
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Conseil, Formation, Développement - Indépendant

    Informations forums :
    Inscription : Février 2010
    Messages : 8 420
    Points : 16 264
    Points
    16 264
    Par défaut
    Bonjour

    La police à utiliser en priorité est arial unicode.

    Mais comme VBA ne gère pas l'unicode hors export il me semble (sauf ajout récent) il faudrait à mon avis importer de façon classique le fichier et retraiter le contenu de l'onglet.
    Chris
    PowerQuery existe depuis plus de 13 ans, est totalement intégré à Excel 2016 &+. Utilisez-le !

    Quand un homme a faim, mieux vaut lui apprendre à pêcher que de lui donner un poisson.
    Confucius

    ----------------------------------------------------------------------------------------------
    En cas de résolution, n'hésitez pas cliquer sur c'est toujours apprécié...

  6. #6
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2017
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : France, Mayenne (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2017
    Messages : 8
    Points : 3
    Points
    3
    Par défaut
    Bonjour et merci à vous deux, j'ai finalement trouvé une solution en contournant le problème. En effet, j'ouvre donc le fichier normalement dans un autre tableur et copie le contenue dans un nouvel onglet du tableur précédent, cela fonctionne je vais garder ca comme ca pour le moment.. ^^
    Merci de votre aide !

Discussions similaires

  1. Caractères "spéciaux" - problème d'import csv
    Par subarud dans le forum SAS Base
    Réponses: 0
    Dernier message: 15/06/2009, 12h02
  2. [HTML 4.0] Caractères chinois : problème enligne-hors ligne
    Par polochtwo dans le forum Balisage (X)HTML et validation W3C
    Réponses: 11
    Dernier message: 20/03/2009, 02h36
  3. [Tomcat 5.5.25] Problème d'affichage de caractères chinois
    Par adeline77 dans le forum Tomcat et TomEE
    Réponses: 6
    Dernier message: 11/07/2008, 15h56
  4. Réponses: 0
    Dernier message: 20/01/2008, 11h12
  5. Problème de codage de caractères depuis l'import
    Par compu dans le forum SQL Procédural
    Réponses: 1
    Dernier message: 22/03/2006, 08h37

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