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
|
Sub CreerIndexAvecNumerosDeListe()
Dim Mot As String, Index As String, MotsIndex() As String
Dim StyleRecherche As String
Dim I As Integer, J As Integer, IndexMatrice As Integer
Dim MonDico As Object 'Scripting.Dictionary
Dim MatriceIndex() As Variant, Temp1 As Variant
MotsIndex = Split("Lorem,veniam,Nemo,magnam,provident,necessitatibus", ",")
Index = ""
StyleRecherche = "Paragraphe de liste" ' Nom du style de numérotation
IndexMatrice = 0
Set MonDico = CreateObject("Scripting.Dictionary")
' Boucle à travers les mots à indexer
For I = LBound(MotsIndex) To UBound(MotsIndex)
Mot = Trim(MotsIndex(I))
' Recherche insensible à la casse et sans tenir compte de la ponctuation
Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
RegEx.Global = True
RegEx.IgnoreCase = True
RegEx.Pattern = "\b" & Mot & "\b"
' Boucle à travers les paragraphes du document
Dim Paragraphe As Paragraph
For Each Paragraphe In ActiveDocument.Paragraphs
' Vérifie si le paragraphe a le style de numérotation souhaité
If Paragraphe.Style.NameLocal = StyleRecherche Then
If RegEx.Test(Paragraphe.Range.Text) Then
' Index = Index & Mot & ": p" & Paragraphe.Range.ListFormat.ListString & vbCrLf
If Not MonDico.Exists(Mot) Then
MonDico.Add (Mot), Mot & " : p" & Paragraphe.Range.ListFormat.ListString
ReDim Preserve MatriceIndex(1, IndexMatrice)
MatriceIndex(0, IndexMatrice) = Mot
MatriceIndex(1, IndexMatrice) = Mot & " : p " & Paragraphe.Range.ListFormat.ListString & ", "
IndexMatrice = IndexMatrice + 1
Else
For J = LBound(MatriceIndex, 2) To UBound(MatriceIndex, 2)
If MatriceIndex(0, J) = Mot Then
MatriceIndex(1, J) = MatriceIndex(1, J) & "p " & Paragraphe.Range.ListFormat.ListString & ", "
Exit For
End If
Next J
End If
End If
End If
Next Paragraphe
Next I
' Tri de la matrice par sa deuxième colonne
For I = LBound(MatriceIndex, 2) To UBound(MatriceIndex, 2) - 1
For J = I + 1 To UBound(MatriceIndex, 2)
If MatriceIndex(1, I) > MatriceIndex(1, J) Then
Temp1 = MatriceIndex(1, I)
MatriceIndex(1, I) = MatriceIndex(1, J)
MatriceIndex(1, J) = Temp1
End If
Next J
Next I
' Insère l'index généré à la fin du document
With ActiveDocument.Content
' .InsertAfter vbCrLf & "INDEX : " & vbCrLf & vbCrLf & Index
.InsertAfter vbCrLf & "INDEX : " & vbCrLf & vbCrLf
For IndexMatrice = LBound(MatriceIndex, 2) To UBound(MatriceIndex, 2)
.InsertAfter Mid(MatriceIndex(1, IndexMatrice), 1, Len(MatriceIndex(1, IndexMatrice)) - 2) & vbCrLf
Next IndexMatrice
End With
Set MonDico = Nothing
End Sub |
Partager