CONVERTIR DU RTF EN HTML , GERE GRAS, ITALIQUE, SOULIGNE, RETOUR CHARIOT, 5 POLI

celiphane Messages postés 466 Date d'inscription samedi 16 février 2002 Statut Membre Dernière intervention 20 avril 2007 - 5 oct. 2002 à 17:05
tontonkika Messages postés 4 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 25 juillet 2005 - 5 déc. 2006 à 13:17
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/4707-convertir-du-rtf-en-html-gere-gras-italique-souligne-retour-chariot-5-polices-7-tailles-toutes-couleurs-et-alignement

tontonkika Messages postés 4 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 25 juillet 2005
5 déc. 2006 à 13:17
Salut à tous,

J'ai pris il y a quelque temps une source sur ce site pour convertir du code RTF en HTML. Je l'ai quelque peu modifiée et je veux vous la faire partager (c'est une fonction liée au contrôle RTF...) :

Public Function ConvertToHTML(Box As Control, Optional FlgAlert As Boolean True, Optional b_netscape4 As Boolean False) As String 'System.Windows.Forms.RichTextBox
' Converti une chaine RTF en HTML
Dim strHTML As String
Dim strColour As String
Dim colorTmp As String
Dim txtTmp As String
Dim lienTmp As String
Dim blnBold As Boolean
Dim blnItalic As Boolean
Dim blnUnderline As Boolean
Dim strFont As String
Dim shtSize As Single 'Short
Dim strAlign As String
Dim numAligne As Integer
Dim lngOriginalStart As Long
Dim lngOriginalLength As Long
Dim intCount As Integer
Dim s_diez As String
Dim i As Integer
Dim Texte As String
Dim Charset As String
Dim CodeASCII As String
Dim CodePage As String
Dim LCID As Long
Dim X() As Byte
Dim Y() As Byte

On Error GoTo ConvertToHTMLError

' On sort si la Box est vide
If Len(Box.Text) = 0 Then
Exit Function
End If

' Stockage et recuperation du premier caractere
lngOriginalStart = 0
lngOriginalLength = Len(Box.Text)
Box.SelStart = 0
Box.SelLength = 1

strHTML = ""

' Initialisation des parametres
strColour = Right$("000000" & Hex(Box.SelColor), 6)
strColour = Right$(strColour, 2) & Mid$(strColour, 3, 2) & Left$(strColour, 2)

' MsgBox Hex(strColour)
blnBold = Box.SelBold
blnItalic = Box.SelItalic
blnUnderline = Box.SelUnderline

strFont = Box.SelFontName
shtSize = Box.SelFontSize
numAligne = Box.SelAlignment
Select Case Box.SelAlignment
Case 0 ' Left
strAlign = "Left"
Case 1 ' right
strAlign = "Right"
Case 2 ' center
strAlign = "Center"
End Select


' ALIGN
'If strAlign <> "" Then
strHTML = strHTML & "
"
'End If

' STYLE
If b_netscape4 Then
strHTML = strHTML & ""
Else
s_diez = "#"

strHTML = strHTML & ""
End If

' Gestion du gras
If blnBold Then
strHTML = strHTML & ""
End If
' Gestion de l'italique
If blnItalic Then
strHTML = strHTML & ""
End If
' Gestion du souligné
If blnUnderline Then
strHTML = strHTML & ""
End If

' premier caractere
'strHTML = strHTML & Box.SelText

' Pour tous les caracteres
intCount = 0
Do While intCount <= Len(Box.Text)
intCount = intCount + 1
'For intCount = 1 To Len(Box.Text)
' caractere courant
Box.SelStart = (intCount - 1)
Box.SelLength = 1

'MsgBox Box.SelText & vbCrLf & vbCrLf & Box.SelRTF

If Len(Box.SelText) > 0 Then
If FlgAlert Then
If Asc(Box.SelText) = 32 And FlgAlert Then
If InStr(Box.SelRTF, "\pict") > 0 Then
If FlgAlert Then
MsgBox "Attention, les images dans les textes, ne seront pas converties", vbCritical, "Attention"
FlgAlert = False
End If
End If
End If
End If
End If

' Gestion du saut de ligne
If Box.SelText Chr$(10) Or Len(Box.SelText) 0 Then
strHTML = strHTML & "
"
intCount = intCount + 1
End If

' Changement de souligné
If Box.SelUnderline <> blnUnderline Then
If Box.SelUnderline = False Then
strHTML = strHTML & ""
End If
End If
' Changement d'italique
If Box.SelItalic <> blnItalic Then
If Box.SelItalic = False Then
strHTML = strHTML & ""
End If
End If
' Changement de gras
If Box.SelBold <> blnBold Then
If Box.SelBold = False Then
strHTML = strHTML & ""
End If
End If

' Eventuel changement destyle
colorTmp = Right$("000000" & Hex(Box.SelColor), 6)
colorTmp = Right$(colorTmp, 2) & Mid$(colorTmp, 3, 2) & Left$(colorTmp, 2)
If colorTmp <> strColour _
Or Box.SelFontName <> strFont _
Or Box.SelFontSize <> shtSize _
Or Box.SelAlignment <> numAligne Then

If b_netscape4 Then
strHTML = strHTML & ""
Else
If blnBold And Box.SelBold Then
strHTML = strHTML & ""
blnBold = False
End If
If blnItalic And Box.SelItalic Then
strHTML = strHTML & ""
blnItalic = False
End If
If blnUnderline And Box.SelUnderline Then
strHTML = strHTML & ""
blnUnderline = False
End If
strHTML = strHTML & ""
End If

' ALIGN
If Box.SelAlignment <> numAligne Then
Select Case Box.SelAlignment
Case 0 ' Left
strAlign = "Left"
Case 1 ' right
strAlign = "Right"
Case 2 ' center
strAlign = "Center"
End Select
strHTML = strHTML & "

"
End If

If b_netscape4 Then
'traitement de la taille du texte
strHTML = strHTML & ""
Else
s_diez = "#"

strHTML = strHTML & ""
End If

End If

' Changement de gras
If Box.SelBold <> blnBold Then
If Box.SelBold Then
strHTML = strHTML & ""
End If
End If
' Changement d'italique
If Box.SelItalic <> blnItalic Then
If Box.SelItalic Then
strHTML = strHTML & ""
End If
End If
' Changement de souligné
If Box.SelUnderline <> blnUnderline Then
If Box.SelUnderline Then
strHTML = strHTML & ""
End If
End If


If Len(Box.SelText) > 0 Then
' test si lien
'lien
If InStr(Box.SelRTF, "\ul") > 0 And InStr(intCount + 1, Box.Text, ">") > 0 And InStr(intCount + 1, Box.Text, ">") < InStr(intCount + 1, Box.TextRTF, "\ulnone") Then
For i = intCount To InStr(intCount + 1, Box.Text, ">")
Box.SelStart = (i - 1)
Box.SelLength = 1
If InStr(Box.SelRTF, "\ul") <= 0 Then
Exit For
End If
Next i
Box.SelStart = (intCount - 1)
Box.SelLength = 1

If i = InStr(intCount + 1, Box.Text, ">") + 1 Then
lienTmp = Mid$(Box.Text, intCount, InStr(intCount + 1, Box.Text, ">") - intCount)
lienTmp = Mid$(lienTmp, InStr(lienTmp, "<") + 1)
' MsgBox Mid$(Box.Text, intCount, InStr(intCount + 1, Box.Text, "<") - intCount)

strHTML = strHTML & "[ " & Mid$(Box.Text, intCount, InStr(intCount + 1, Box.Text, "<") - intCount) & "]"
intCount = InStr(intCount + 1, Box.Text, ">")
End If


ElseIf Box.SelText "?" And InStr(Box.SelRTF, " ?}") 0 Then
'If InStr(Box.SelRTF, " ?}") = 0 Then
MsgBox "caractère étranger : " & vbCrLf & Box.SelRTF
Charset = Mid$(Box.SelRTF, InStr(Box.SelRTF, "\fcharset") + 9)
Charset = Left$(Charset, InStr(Charset, " ") - 1)
If InStr(Box.SelRTF, "\'") > 0 Then
CodeASCII = Mid$(Box.SelRTF, InStr(Box.SelRTF, "\'") + 2)
CodeASCII = Left$(CodeASCII, InStr(CodeASCII, "}") - 1)
CodeASCII = "&h0" & Replace(CodeASCII, "\'", "&h0")
'MsgBox "" & Chr$(Val(Right$(CodeASCII, 5)))
'MsgBox "" & StrConv(Chr$(Val(Left$(CodeASCII, 5))) & Chr$(Val(Right$(CodeASCII, 5))), vbUnicode, 2052)
Select Case Charset
Case 0 ' Ansi
LCID = 0
Case 1 ' Default
LCID = 0
Case 2 ' Symbol
LCID = 0
Case 3 ' Invalid
LCID = 0
Case 77 ' Mac
LCID = 0
Case 128 ' Shift Jis
LCID = 0
CodePage = 932
Case 129 ' Hangul
LCID = 0
Case 130 ' Johab
LCID = 0
Case 134 ' GB2312
LCID = 2052
CodePage = 936
Case 136 ' Big5
LCID = 1028
CodePage = 950
Case 161 ' Greek
LCID = 0
CodePage = 1253
Case 162 ' Turkish
LCID = 0
CodePage = 1254
Case 163 ' Vietnamese
LCID = 0
CodePage = 1258
Case 177 ' Hebrew
LCID = 0
Case 178 ' Arabic
LCID = 0
CodePage = 1256
Case 179 ' Arabic Traditional
LCID = 0
Case 180 ' Arabic user
LCID = 0
Case 181 ' Hebrew user
LCID = 0
Case 186 ' Baltic
LCID = 0
Case 204 ' Russian
LCID = 0
Case 222 ' Thai
LCID = 0
CodePage = 874
Case 238 ' Eastern European
LCID = 0
Case 254 ' PC 437
LCID = 0
Case 255 ' OEM
LCID = 0
Case Else
MsgBox "Attention la langue utilisée dans le texte n'est pas supportée : Charset=" & CStr(Charset)
LCID = 0
End Select

ReDim X(1)
X(0) = Val(Left$(CodeASCII, 5))
X(1) = Val(Right$(CodeASCII, 5))
Y = StrConv(X, vbUnicode, LCID) ' Convert string.
CodeASCII = "&h0" & Hex$(Y(1)) & Hex$(Y(0))
Else
CodeASCII = Mid$(Box.SelRTF, InStr(Box.SelRTF, "?}") - 5, 5)
End If

strHTML = strHTML & "&#" & CStr(Val(CodeASCII)) & ";"
intCount = intCount + 1

Else ' Ajout du caractere
txtTmp = Box.SelText 'Mid$(Box.Text, intCount, 1)
' MsgBox "" & Asc(Mid$(Box.Text, intCount, 1))
Select Case txtTmp
Case "<"
strHTML = strHTML & "<"
Case Chr$(13), Chr$(10)
strHTML = strHTML & ""
Case " "
If Right$(strHTML, 1) = " " Then
strHTML = Left$(strHTML, Len(strHTML) - 1) & " "
ElseIf Right$(strHTML, 6) = " " Or Box.SelStart < 1 Then
strHTML = strHTML & " "
Else
strHTML = strHTML & " "
End If
Case Else
strHTML = strHTML & txtTmp
End Select

End If
'strHTML = strHTML & Mid$(Box.Text, intCount, 1)
End If

' MAJ
strColour = Right$("000000" & Hex(Box.SelColor), 6)
strColour = Right$(strColour, 2) & Mid$(strColour, 3, 2) & Left$(strColour, 2)
blnBold = Box.SelBold
blnItalic = Box.SelItalic
blnUnderline = Box.SelUnderline

strFont = Box.SelFontName
shtSize = Box.SelFontSize
numAligne = Box.SelAlignment
Select Case Box.SelAlignment
Case 0 ' Left
strAlign = "Left"
Case 1 ' right
strAlign = "Right"
Case 2 ' center
strAlign = "Center"
End Select
Loop
'Next intCount

' Fermeture des tags bold/italic...
If blnBold Then strHTML = strHTML & ""
If blnItalic Then strHTML = strHTML & ""
If blnUnderline Then strHTML = strHTML & ""

' On ferme le tag SPAN
If b_netscape4 Then
strHTML = strHTML & ""
Else
strHTML = strHTML & ""
End If

' ALIGN
'If strAlign <> "" Then
strHTML = strHTML & "
"
'End If

' Restauration de l'original richtextbox
'Box.Select lngOriginalStart, lngOriginalLength


' Gestion des liens HTML du type
' $£$http://www.yahoo.fr$£$cliquez ici$£$
Do While InStr(strHTML, "$£$") > 0
i = InStr(strHTML, "$£$")
If InStr(i + 1, strHTML, "$£$") > 0 Then
'cherche la troisième balise
If InStr(InStr(i + 1, strHTML, "$£$"), strHTML, "$£$") > 0 Then
Texte = Mid$(strHTML, i + 3)
Texte = Left$(Texte, InStr(Texte, "$£$") - 1) & "' target='_blank'>" & Mid$(Texte, InStr(Texte, "$£$") + 3)
strHTML = Left$(strHTML, i - 1) & "" & Mid(strHTML, i + 3)
End If
End If
Loop

ConvertToHTML = strHTML

Exit Function
ConvertToHTMLError:

MsgBox Err.Description & vbCrLf & Err.Number, vbCritical, LoadResString(185 + Langage)
Resume Next

End Function
cbnet Messages postés 176 Date d'inscription vendredi 29 octobre 2004 Statut Membre Dernière intervention 7 mars 2005
16 févr. 2005 à 18:16
Encore toi celiphane ! decidement tes sources sont excellentes. Bravo 10/10
DeadlyPredator Messages postés 222 Date d'inscription jeudi 15 janvier 2004 Statut Membre Dernière intervention 30 juin 2008
5 mai 2004 à 05:22
check .créer un fichier rtf avec wordpad, enregistre le pi regarde ça source avec bloc-note. tu va voir. c simple. un en-tête, une table des couleurs, des lignes pour chaque paragraphe, ... je pense que je vais créer un VRAI convertisseur.
DeadlyPredator Messages postés 222 Date d'inscription jeudi 15 janvier 2004 Statut Membre Dernière intervention 30 juin 2008
5 mai 2004 à 05:16
bien mais ya des rfc sur le rtf. je crois que ça doit pas être si complexe. Reste que fallait y penser.
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
19 févr. 2004 à 10:11
Dommage que tu n'aies pas prévu que les fichiers RTF puissent dépasser les 32767 caractères (For Next avec i déclaré comme Integer --> Passe-le en Long)
Beau boulot
celiphane Messages postés 466 Date d'inscription samedi 16 février 2002 Statut Membre Dernière intervention 20 avril 2007
14 nov. 2003 à 22:31
remarque très pertinante ;)
cs_dlaudrin Messages postés 1 Date d'inscription vendredi 3 janvier 2003 Statut Membre Dernière intervention 14 novembre 2003
14 nov. 2003 à 18:20
si l'utilité est pour le web pourquoi tu n'utilise pas le dhtml et je javascript c'est bien plus pratique. Sinon c'est cool
cs_Ouneufe Messages postés 60 Date d'inscription mardi 4 septembre 2001 Statut Membre Dernière intervention 19 janvier 2009
12 nov. 2003 à 06:46
très bon
celiphane Messages postés 466 Date d'inscription samedi 16 février 2002 Statut Membre Dernière intervention 20 avril 2007
10 juin 2003 à 22:22
salut,
merci,
mais non, je n'ai pas (encore) d'info la dessus ! post un msg si tu y touches ! @+
cs_gogomanu Messages postés 29 Date d'inscription mardi 7 janvier 2003 Statut Membre Dernière intervention 26 mars 2009
10 juin 2003 à 14:14
Salut ! C'est tout à fait excellent, 10/10.
J'ai en projet de faire un client mail, j'ai déjà le code d'envoi et de génération avec pièces jointes, images du html intégrées (cid) dans le mail, et tout le toutim et il me manque la partie "éditeur", alors ton prog va surement m'aider si je choisis le RTF pour générer le HTML. Je sais que l'on peut faire des images en RTF mais j'ai pas encore trouvé comment ! Si tu as des infos là dessus ?
celiphane Messages postés 466 Date d'inscription samedi 16 février 2002 Statut Membre Dernière intervention 20 avril 2007
7 oct. 2002 à 13:21
Merci pour tous vos encouragements,

leptidev => avec plaisir : celiphane@oreka.com
Sirocooo => tu veux dire en prenant directement un ".doc" sur le disque ? faire une conversion automatique .doc -> .html ? Très bonne idée, mais je ne vais pas m'y attarder car ce prog correspond aux attentes dont m'a fait part la personne pour qui je l'ai développé, cad conversion vers le presser-papier... Il est donc grandement améliorable, faites en ce que vous voulez, mais en ce qui me concerne, je repart vers d'autres occupations !
iubito => idem

Alors bon courage à tous ! @+ Tchao
Sirocooo Messages postés 412 Date d'inscription mercredi 19 décembre 2001 Statut Membre Dernière intervention 7 avril 2008 1
7 oct. 2002 à 12:52
très bon boulot 9/10 bravo
si tu pouvait faire la même chose en partant d'un ficheir sur disque.
leptidev Messages postés 206 Date d'inscription mercredi 30 janvier 2002 Statut Membre Dernière intervention 4 décembre 2007
7 oct. 2002 à 12:41
Salut, ta source est vraiment géniale, je bosse en ce moment et depuis presque 1 an sur un éditeur HTML, je pourrais utiliser ta source en notitiant bien sur ton mail sur mon PROG?

Merci beaucoup dans tous les cas
cs_iubito Messages postés 629 Date d'inscription mercredi 3 juillet 2002 Statut Membre Dernière intervention 9 octobre 2006
7 oct. 2002 à 09:31
ingénieux en effet :)
Tu pourrais en plus prévoir une sauvegarde dans un fichier HTML et comme ça t'aura fait un éditeur html ;D !
Setaou Messages postés 127 Date d'inscription mercredi 28 mars 2001 Statut Membre Dernière intervention 4 octobre 2004
6 oct. 2002 à 15:15
ingénieux et utile ! bravo !! 10/10 :)
celiphane Messages postés 466 Date d'inscription samedi 16 février 2002 Statut Membre Dernière intervention 20 avril 2007
5 oct. 2002 à 17:05
Alors, un p'tit commentaire pour la route ? Pis une ch'tite note ? merci ;) !
Rejoignez-nous