par Michael DELIQUE » 28 Déc 2004 à 10:01
- Code : Tout sélectionner
Function Spliting(Byval wSource As String,Byval wnbChar As Integer,Byval wsgWord As Single, wnbSplit As Integer) As Variant
'wtext = text a découper
'wnbChar = nombre maximum de caractère par troncon
'wvrWord = true, ne coupe pas un mot en 2, false, coupe le mot de 2
'nbSplit indique le nombre maximal de ligne retoruné, si zero alors renvoi autant de lighne que nécessaire
'Déclaration des Variables
Dim textTempo As String
Dim textTempo2 As String
Dim lstSplit List As String
Dim i As Long
Dim J As Long
Dim vrValue As Variant
On Error Goto ErreurSpliting
Erase LstSplit
If wSource = "" Then
LstSplit(0) = ""
Spliting = LstSplit
Erase LstSplit
Exit Function
End If
If wnbChar < 1 Then
LstSplit(0) = ""
Spliting = LstSplit
Erase LstSplit
Exit Function
End If
If Len(wSource) <= wnbChar Then
LstSplit(0) = wSource
Spliting = LstSplit
Erase LstSplit
Exit Function
End If
Select Case wsgWord
Case True 'avec respect des mots
i = 0
TextTempo = wSource
While TextTempo<>""
If wnbSplit > 0 Then
If i >= wnbSplit Then
Spliting = LstSplit
Erase LstSplit
Exit Function
End If
End If
If (Len(TextTempo)-wnbChar)>0 Then
TextTempo2 = Left(TextTempo,wnbChar)
TextTempo = Right(TextTempo,Len(TextTempo)-wnbChar)
'détermine si pour le trocon de chaine, le dernier mot est coupé ou entier
'en fonction du premier caratère restant sur le texte d'origine à découpé
Select Case Left(TextTempo,1)
Case " ",Chr(9),Chr(10),Chr(13)
'le mot est entier aucun traitement à faire !
Case Else
'le mot n'est pas entier
vrValue = False
J = Len(TextTempo2)
'recherche du prochain caratère de séparation de mot
While vrValue = False
Select Case Mid(TextTempo2,J,1)
Case " ",Chr(9),Chr(10),Chr(13)
vrValue = True
Case Else
J = J-1
If J = 0 Then
vrValue = True
End If
End Select
Wend
End Select
'si J = 0 c'est que le troncon ne comporte pas d'espace
If J = 0 Then
LstSplit(i) = TextTempo2
Else
LstSplit(i) = Left(TextTempo2,J)
TextTempo = Right(TextTempo2,Len(TextTempo2)-J) + TextTempo
End If
TextTempo2 = ""
J = 0
Else
LstSplit(i) = TextTempo
TextTempo = ""
End If
i = i + 1
Wend
Case False ' sans respect des mots
i = 0
TextTempo = wSource
While TextTempo<>""
If wnbSplit > 0 Then
If i >= wnbSplit Then
Spliting = LstSplit
Erase LstSplit
Exit Function
End If
End If
If (Len(TextTempo)-wnbChar)>0 Then 'détermin e si la taille du texte a découpé est supérieur a la longeur maximal
LstSplit(i) = Left(TextTempo,wnbChar)
TextTempo = Right(TextTempo,Len(TextTempo)-wnbChar)
Else
' si la taill est inférieur alors on est arrivé a la fin du découpage !
LstSplit(i) = TextTempo
TextTempo = ""
End If
i = i + 1
Wend
End Select
i = 0
J = 0
TextTempo = ""
TextTempo2 = ""
Spliting = LstSplit
Erase LstSplit
Exit Function
ErreurSpliting:
Msgbox "(Spliting) Erreur " + Str(Err) + " : "+Chr(10) + Cstr(Error)+". "+Chr(10)+"Ligne N° "+Cstr(Erl),16," ERREUR !"
LstSplit(0) = ""
Spliting = LstSplit
Exit Function
End Function
Cordialement
Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN