VBA Wörter auflisten und zählen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • VBA Wörter auflisten und zählen

    Hi,
    möchte eine Liste mit allen vorhanden Wörtern aus einem Text erstellen und dahinter die Anzahl angeben wie oft jedes Wort im Text vorkommt.
    Finde keinen Ansatz wie ich die Sache angehen kann, dazu habe ich nur wenig Kenntnisse in VBA.
    Gibt es vielleicht hierfür schon fertige Lösungen?
    Gruß
    mactoni
  • Quellcode

    1. Sub AnzahlWorte()
    2. Dim rngRange As Word.Range
    3. Dim lngAnzahlWorte As Long
    4. Dim strText As String
    5. Dim lngL As Long
    6. Dim strDasWort4 As String
    7. Dim lngWortNr As Long
    8. Set rngRange = ActiveDocument.Range
    9. lngAnzahlWorte = rngRange.ComputeStatistics(wdStatisticWords)
    10. 'MsgBox lngAnzahlWorte, , "Anzahl Worte im Doument"
    11. strText = rngRange.Text
    12. strText = Replace(strText, ".", " ", 1, -1, 1)
    13. strText = Replace(strText, ",", " ", 1, -1, 1)
    14. strText = Replace(strText, "?", " ", 1, -1, 1)
    15. strText = Replace(strText, "!", " ", 1, -1, 1)
    16. strText = Replace(strText, ":", " ", 1, -1, 1)
    17. strText = Replace(strText, vbCr, " ", 1, -1, 1)
    18. strText = Replace(strText, vbLf, " ", 1, -1, 1)
    19. strText = Replace(strText, "(", " ", 1, -1, 1)
    20. strText = Replace(strText, ")", " ", 1, -1, 1)
    21. strText = Replace(strText, Chr(11), " ", 1, -1, 1)
    22. strText = Replace(strText, Chr(7), " ", 1, -1, 1)
    23. 'ggf weitere Zeichen ergänzen
    24. strText = Trim(strText)
    25. Do While Len(strText) <> lngL
    26. lngL = Len(strText)
    27. strText = Replace(strText, " ", " ", 1, -1, 1)
    28. Loop
    29. lngAnzahlWorte = UBound(Split(strText, " ", -1, 1))
    30. MsgBox lngAnzahlWorte, , "Anzahl Worte im Doument"
    31. 'wie oft kommt das 4. Wort im Documet im Gesamten Text vor:
    32. lngWortNr = 1
    33. strDasWort4 = Split(strText, " ", -1, 1)(lngWortNr - 1)
    34. lngAnzahlWorte = (Len(strText) - Len(Replace(strText, strDasWort4, "", 1, -1, 1))) / Len(strDasWort4)
    35. MsgBox lngAnzahlWorte, , "So oft gibts das Wort: " & strDasWort4
    36. End Sub
    Alles anzeigen


    Gruß
    mactoni