par , 10/05/2016 à 12h22 (1965 Affichages)
Bonjour à tous
Problème qui intervient quand on veut écrire directement dans les fichiers HTML qui sont donc encodés UTF-8 sans BOM.
Il n'est pas question de modifier dynamiquement par VBA le DOM cf par exemple l'excellent tutoriel de Qwazerty ici mais bien de manipuler un fichier encodé UTF-8 sans BOM et dans ce cas pratique d'ajouter / modifier du texte dans un fichier.
Pour notre exemple
Côté HTML
Prendre un fichier HTML quelconque. Tous les fichier HTML sont encodés par défaut UTF-8 sans BOM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!-- saved from url=(0016)http://localhost -->
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow:hidden;">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Map</title>
<link href = "http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css " rel = "stylesheet" type="text/css"/>
<script type="text/javascript" src = "http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src = "http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="http://maps.googleapis.com/maps/api/js"></script>
</head>
<body>
<div id="Map"></div>
</body>
</html> |
IMPORTANT pour la compréhension code VBA : La ligne après <script src="http://maps.googleapis.com/maps/api/js"></script> a pour indice 12 si on commence à 0.
Côté Access
Créer un formulaire avec un bouton et ajouter sous l'événement click . Se rappeler comme l'explique zenpbb en réponse à un post sur la même problématique que l'encodage UFT-8 ajoute des caractères (EF BB BF) en début de la 1ère ligne d''où le traitement
arrayString(0) = Right(arrayString(0), Len(arrayString(0)) - 3)
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
| Private Sub cmdBtn_Click()
Dim fsT As Object
Dim arrayString As Variant
Dim stringChar As String
sPath = "monChemin\monFichierHTML"
'lecture du fichier et ajout dans un array un item correspondant à une ligne finie par vbCr & vbLf. => X lignes finies par vbCr & vbLf = X items dans arrayString
arrayString = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath).ReadAll, vbCr & vbLf)
'Comme l'encodage dans OpenTextFile() est UFT-8, il y a ajout des caractères (EF BB BF) en début de la 1ère ligne comme précisé par zenpbb d'où ce traitement
arrayString(0) = Right(arrayString(0), Len(arrayString(0)) - 3)
arrayString(12) = "Mon texte éphémère à ajouter en ligne 13 soit l'indice 12. voir Important ci-dessous" 'Existe des caractères spéciaux comme é et è
stringChar = Join(arrayString, vbCr & vbLf) 'join concatene an array in a string variable
Set fsT = CreateObject("ADODB.Stream")
fsT.type = 2 'Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" 'Specify charset For the source text data.
fsT.Open 'Open the stream And write binary data To the object
fsT.WriteText stringChar
fsT.SaveToFile sPath, 2 'Save binary data
end sub |
Et voilà