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
|
Private Sub gestionWord(ByVal text As String)
Dim balises As String() = {"<strong>", "<souligne>", "<i>"}
doc = New Word.Document
Dim cptLettres As Integer = 0
doc = objWord.Documents.Add()
With objWord
.Visible = True
.ActiveWindow.Selection.Font.Name = "Arial"
.ActiveWindow.Selection.Font.Size = 8
With .ActiveWindow.Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.FirstLineIndent = -3.13
End With
.ActiveWindow.ActivePane.VerticalPercentScrolled = 0
With .ActiveWindow.Selection.ParagraphFormat
.LeftIndent = 0.75
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
.ActiveWindow.ActivePane.VerticalPercentScrolled = 0
End With
recopiage(text, 0, text.Length - 1, False, False, False)
End Sub
Private Sub recopiage(ByVal text, ByVal depart, ByVal arrivee, Optional ByVal boolStrong = False, Optional ByVal boolI = False, Optional ByVal boolSouligne = False)
Dim balises As String() = {"<strong>", "<souligne>", "<i>"}
'Dim sousTexte As String = text.SubString(depart, arrivee - depart)
'tableaux:
'0: debut du texte de la balise
'1: fin du texte de la balise
'2: booléen strong
'3: booléen italique
'4: booléen underline
'5: nom balise
Dim tabStrong(6), tabI(6), tabSouligne(6)
Dim siStrong, siI, siSouligne As Boolean
For i = depart To arrivee
Dim sousTexte = text.SubString(i, arrivee - i)
Dim bool As Boolean = False
Dim premiereBalise As Integer = arrivee
'''''''''''''''''''''''''''''''''''''''''
'à tester i => i - arrivee
'''''''''''''''''''''''''''''''''''''''''
'on regarde ce que contient la chaine
For Each balise In balises
If sousTexte.Contains(balise) Then
'on recherche la toute première balise afin de coller la première partie du texte
If text.IndexOf(balise, i) < premiereBalise Or premiereBalise = -1 Then
premiereBalise = text.IndexOf(balise, i)
End If
'repérer la fin de balise
Select Case balise
Case "<strong>"
tabStrong(0) = text.IndexOf(balise, i) + balise.Length
tabStrong(1) = text.IndexOf(balise.Replace("<", "</"), i)
tabStrong(2) = True
tabStrong(3) = boolI
tabStrong(4) = boolSouligne
tabStrong(5) = balise
siStrong = True
Case "<i>"
tabI(0) = text.IndexOf(balise, i) + balise.Length
tabI(1) = text.IndexOf(balise.Replace("<", "</"), i)
tabI(2) = boolStrong
tabI(3) = True
tabI(4) = boolSouligne
tabI(5) = balise
siI = True
Case "<souligne>"
tabSouligne(0) = text.IndexOf(balise, i) + balise.Length
tabSouligne(1) = text.IndexOf(balise.Replace("<", "</"), i)
If tabSouligne(1) = -1 Then
Debug.Print(balise.Replace("<", "</"))
Debug.Print(text.ToString.Substring(1649, 100))
End If
tabSouligne(2) = boolStrong
tabSouligne(3) = boolI
tabSouligne(4) = True
tabSouligne(5) = balise
siSouligne = True
End Select
Debug.Print("=============================================================================")
Debug.Print(text.ToString.Substring(text.IndexOf(balise, i) + balise.Length, text.IndexOf(balise.Replace("<", "</"), i) - text.IndexOf(balise, i) + balise.Length))
bool = True
End If
Next
'on insère ce qu'il y a avant la balise
doc.ActiveWindow.Selection.Font.Bold = boolStrong
doc.ActiveWindow.Selection.Font.Italic = boolI
doc.ActiveWindow.Selection.Font.Underline = boolSouligne
doc.ActiveWindow.Selection.TypeText(text.Substring(i, premiereBalise - i))
doc.ActiveWindow.Selection.Font.Bold = False
doc.ActiveWindow.Selection.Font.Italic = False
doc.ActiveWindow.Selection.Font.Underline = False
'on vérifie qu'une balise est bien présente
If bool Then
'récupération puis utilisation de la première balise
Dim commencements As New List(Of Integer)
If siStrong Then
commencements.Add(tabStrong(0))
End If
If siI Then
commencements.Add(tabI(0))
End If
If siSouligne Then
commencements.Add(tabSouligne(0))
End If
commencements.Sort()
Dim tabPremiereBalise() = minimum(commencements, tabStrong, tabI, tabSouligne)
i = tabPremiereBalise(1) + tabPremiereBalise(5).Length
recopiage(text, tabPremiereBalise(0), tabPremiereBalise(1), tabPremiereBalise(2), tabPremiereBalise(3), tabPremiereBalise(4))
Else
i = arrivee
End If
Next
End Sub
Private Function minimum(ByVal tab, ByRef tabStrong, ByRef tabI, ByRef tabSouligne) As Object()
If tabStrong(0) = tab(0) Then
Return tabStrong
End If
If tabI(0) = tab(0) Then
Return tabI
End If
If tabSouligne(0) = tab(0) Then
Return tabSouligne
End If
End Function |
Partager