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
|
Option Explicit
Sub Chercher_AB_CD()
Dim ChainesATester As Variant
ChainesATester = Array("AB", "Commentaire AB", "CD", "Commentaire CD")
Essai_commenter2 ChainesATester
End Sub
Sub Essai_commenter2(ChainesATester2 As Variant)
Dim DocEnCours As Document
Dim I As Integer, J As Integer
Dim MatriceTexte() As Variant
Dim IndexMatrice As Long
Dim CaractereDebut As Long, CaractereFin As Long
Set DocEnCours = ActiveDocument
With DocEnCours
For I = .Comments.Count To 1 Step -1
.Comments(I).Delete
Next I
IndexMatrice = 0
For I = 1 To .Words.Count
With .Words(I)
For J = LBound(ChainesATester2) To UBound(ChainesATester2) Step 2
If InStr(1, .Text, ChainesATester2(J), vbTextCompare) > 0 Then
.Select
With Selection
.HomeKey Unit:=wdStory, Extend:=wdExtend
CaractereDebut = .Characters.Count
CaractereFin = .Characters.Count + 2
ReDim Preserve MatriceTexte(2, IndexMatrice)
MatriceTexte(0, IndexMatrice) = CaractereDebut
MatriceTexte(1, IndexMatrice) = CaractereFin
MatriceTexte(2, IndexMatrice) = ChainesATester2(J + 1)
IndexMatrice = IndexMatrice + 1
End With
End If
Next J
End With
Next I
End With
If IndexMatrice = 0 Then Exit Sub
For IndexMatrice = UBound(MatriceTexte, 2) To LBound(MatriceTexte, 2) Step -1
DocEnCours.Select
With Selection
.SetRange Start:=MatriceTexte(0, IndexMatrice), End:=MatriceTexte(1, IndexMatrice)
.Select
.Comments.Add Range:=Selection.Range, Text:=MatriceTexte(2, IndexMatrice)
' MsgBox Selection.Range.Text & ", " & MatriceTexte(0, IndexMatrice) & ", " & MatriceTexte(1, IndexMatrice)
End With
Next IndexMatrice
Set DocEnCours = Nothing
End Sub |
Partager