La version 2 de terminator est intéressante mais elle reste très difficile à lire car il y a beaucoup de lignes de codes similaires. On a procéder dans terminator2 à deux recherches/remplacements avec quasiment les mêmes options.
Chaque recherche nécessite un code écrit sur 15 lignes (6 à 20 pour la 1ere recherche, 22 à 36 pour la 2eme recherche).
Sub terminator2()
' terminator Macro
' tue espace
reponse = MsgBox("Voulez-vous vraiement lancer le terminator ?", vbYesNo,"Avertissement")
If (reponse = vbYes) Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
end if
End Sub
On va utiliser la notion de procédure qui permet de "regrouper" des lignes similaires. Nous allons créer la procédure rechercherEtRemplacerSansCaracteresGeneriques qui possèdent 2 paramètres : texteAChercher et texteDeRemplacement.
Sub rechercherEtRemplacerSansCaracteresGeneriques(texteAChercher,texteDeRemplacement) Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = texteAChercher .Replacement.Text = texteDeRemplacement .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub
Voici le code de terminator3.
On remarquera que les chaque recherche invoquée par la macro se réduit à une seule ligne. Il suffit de mettre dans le premier paramètre de la procédure le texte à chercher, et de mettre dans le second paramètre le texte à remplacer. Il sera facile avec cette version de rajouter d'autres recherches/remplacements comme dans la macro nettoyageMinimum.
Sub terminator3()
reponse = MsgBox("Voulez-vous vraiement lancer le terminator ?", vbYesNo, "Avertissement")
If (reponse = vbYes) Then
Call rechercherEtRemplacerSansCaracteresGeneriques("^#", "")
Call rechercherEtRemplacerSansCaracteresGeneriques(" ","")
End if
End Sub
Sub rechercherEtRemplacerSansCaracteresGeneriques(texteAChercher,texteDeRemplacement)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = texteAChercher
.Replacement.Text = texteDeRemplacement
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub