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
|
Public Function StatsMots()
Dim oIdx As Collection
Dim i As Long, lCountMots As Long, lTotal As Long
Dim aiCounts() As Integer
Dim sMot As String
lCountMots = GetStatsMots(oIdx, aiCounts)
Debug.Print "Compte mots différents :", lCountMots
'Calcul le total de mots
For i = 1 To lCountMots
lTotal = lTotal + aiCounts(i)
Next i
Debug.Print "Nombre total calculé de mots :", lTotal
Debug.Print "Nombre total de mots d'après word :", _
ActiveDocument.ComputeStatistics(wdStatisticWords, True)
'Cherche le nombre d'occurences d'un mot
sMot = "Bonjour"
Debug.Print "Occurence du mot '" & sMot & "' : " & GetOccurences(sMot, oIdx, aiCounts)
sMot = "un"
Debug.Print "Occurence du mot '" & sMot & "' : " & GetOccurences(sMot, oIdx, aiCounts)
'Reset la collection
Set oIdx = Nothing
End Function
'Compte les occurences d'un mot
Private Function GetOccurences(ByVal sMot As String, ByRef oIdx As Collection, _
ByRef aiCounts() As Integer) As Integer
On Error Resume Next
Dim lPos As Long
lPos = Val(oIdx(Trim$(sMot)))
If Not Err Then
GetOccurences = aiCounts(lPos)
End If
End Function
'Compute les stats
Private Function GetStatsMots(ByRef oIdx As Collection, _
ByRef aiCounts() As Integer) As Long
On Error Resume Next
Dim vMot As Variant
Dim sMot As String
Dim lPos As Long
Set oIdx = New Collection
ReDim aiCounts(0 To 0)
For Each vMot In ActiveDocument.Words
sMot = RTrim(vMot)
If Asc(sMot) > 32 Then
lPos = Val(oIdx(sMot))
If Err.Number > 0 Then
Err.Clear
lPos = UBound(aiCounts) + 1
ReDim Preserve aiCounts(lPos)
aiCounts(lPos) = 1
oIdx.Add lPos & "|" & sMot, sMot
Else
aiCounts(lPos) = aiCounts(lPos) + 1
End If
End If
Next vMot
GetStatsMots = oIdx.Count
End Function |
Partager