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

VBA Access Discussion :

Ecriture de caractères spéciaux dans un fichier UFT-8 sans BOM avec VBA [AC-2010]


Sujet :

VBA Access

  1. #1
    Membre éprouvé

    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    1 026
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 1 026
    Billets dans le blog
    45
    Par défaut Ecriture de caractères spéciaux dans un fichier UFT-8 sans BOM avec VBA
    Bonjour,

    Je récupère des données d'un recordSet pour en faire un tableaux de données au format JavaScript que j'ajoute à un fichier html. Mais problème, avec les caractères spéciaux sont mal interprétés dans l'html.

    Côté Access
    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
     
    Private Sub cmdGeoloc2_Click()
        Dim sPath, strHtml, markers, sSQL As String
        Dim i As Integer
        Dim TextString As Variant
        Dim marker As clsMarker
     
     
        sSQL = "SELECT ...."
     
        Call ecrireFichier(sSQL)
        Set rdSet = CurrentDb.OpenRecordset(sSQL)
        rowNbr = rdSet.RecordCount
     
     
        Do While Not rdSet.EOF = True
            marker.lat = rdSet![LatUs]
            marker.lng = rdSet![LongUS]
            marker.titre = rdSet![name]
            marker.info = rdSet![chain name]
            marker.type = "H"
     
            If Not (IsNull(marker.lat) Or IsNull(marker.lng)) Then
                With marker
                    markers = markers & "{'lat': " & .lat & ","
                    markers = markers & "'lng' : " & .lng & ","
                    markers = markers & "'titre': '" & .titre & "',"
                    'markers = markers & """info"" :'true'," & vbCrLf
                    markers = markers & "'info': '" & .info & "',"
                    markers = markers & "'type' :'" & .type & "'},"
                End With
            End If
            rdSet.MoveNext
        Loop
     
        rdSet.Close
     
        If rowNbr > 0 Then
            strHtml = "var arrMarkers= [" & Left(markers, Len(markers) - 1) & "];"
        End If
     
     
     
        ' Open read handle.
        sPath = "E:\GeoCoding\geoloc2.html"
     
        'read text from file
        TextString = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath).ReadAll, Chr(13) & Chr(10))
     
        'change your text here as TextString(#Line - 1) = "Text still assume you want to replace line 5
        TextString(16) = strHtml
     
        'write back in file
        CreateObject("Scripting.FileSystemObject").CreateTextFile(sPath).Write (Join(TextString, Chr(13) & Chr(10)))
     
     
    End Sub
    Avec pour résultat dans le fichier html

    var arrMarkers= [{'lat': 47.2152,'lng' : -1.54694,'titre': Centre Cit�','info': 'info1','type' :'H'},
    {'lat': 47.207,'lng' : -1.54846,'titre': 'Centre Bord de Loire','info': 'info2','type' :'H'}];


    Merci par avance pour votre aide

  2. #2
    Membre éprouvé

    Profil pro
    Inscrit en
    Janvier 2010
    Messages
    1 026
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 1 026
    Billets dans le blog
    45
    Par défaut
    Solution trouvée et testée !!!

    Comme
    1. StrConv(strText, vbUnicode) produces UTF-16 (the default Windows Unicode format) not UTF-8
    2. Set objTextStream = objFSO.CreateTextFile(strFile, , True) outputs as UTF-16 not UTF-8


    j'ai utilisé CreateObject("ADODB.Stream") mais comme l'explique zenpbb (voir ici)

    (sic)...mais il me semble que la différence entre utf-8 avec et sans BOM se réduit à la présence de 3 octets spécifiques en tête du fichier (EF BB BF).
    Pourquoi alors ne pas réouvrir ton fichier créé en utf-8 avec BOM, vérifier que les 3 premiers octets correspondent bien et dans ce cas le réenregistrer sans ces 3 octets ?...
    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
    Private Sub appendHtmlFile(strHtml As String)
     
        Dim fsT As Object
        Dim arrayString As Variant 'Use varray to manage array dimension
        Dim stringChar As String
     
        ' Open read handle.
        sPath = "monChemin\monfichier.html"
     
     
        'read text from file
         arrayString = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath).ReadAll, vbCr & vbLf)
     
        'change your text here as TextString(#Line - 1) = "Text
         arrayString(0) = Right(arrayString(0), Len(arrayString(0)) - 3) 'ici nettoyage des caractères (EF BB BF) comme précisé par zenpbb
         arrayString(16) = strHtml
     
        'write back in file
        stringChar = Join(arrayString, vbCr & vbLf)
     
        Set fsT = CreateObject("ADODB.Stream")
        fsT.type = 2 
        fsT.Charset = "utf-8" 
        fsT.Open 
        fsT.WriteText stringChar
        fsT.SaveToFile sPath, 2
    end sub



+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 03/11/2009, 12h09
  2. Conversion des caractères spéciaux sur un événement onChange
    Par imagonem dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 10/04/2009, 22h45
  3. Conversion des caractères spéciaux insérés
    Par marty499 dans le forum Langage
    Réponses: 1
    Dernier message: 02/06/2008, 17h18
  4. Conversion des caractères spéciaux en &xyz;
    Par thomzon dans le forum Développement Web en Java
    Réponses: 4
    Dernier message: 05/09/2007, 08h35
  5. Réponses: 2
    Dernier message: 15/06/2007, 16h24

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