Consulting

Results 1 to 8 of 8

Thread: Find repeated word(s) in selected text

  1. #1

    Find repeated word(s) in selected text

    Is there a way to have a macro examine selected text to find the duplicate words in the selection? If the code could be tuned to ignore words 3 letters and under, that would be amazing. Thank you for any help!

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    What constitutes 'duplicate words' in this context? For example, do they have to be immediately one after the other, or can there be other, intervening, words?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,354
    Location
    Lots of ways. Here is one using a dictionary class:

    Option Explicit
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/10/2017
    Dim oWord As Range
    Dim strWord As String
    Dim oDicWords As Object
    Dim lngIndex As Long
      Set oDicWords = CreateObject("Scripting.Dictionary")
      oDicWords.CompareMode = vbBinaryCompare 'Test and test are unique words.
      'oDicWords.CompareMode = vbTextCompare 'Test and test are the same word.
      Set oDicWords = CreateObject("Scripting.Dictionary")
      For Each oWord In Selection.Range.Words
        strWord = Trim(oWord)
        If Len(strWord) > 3 Then
          If Not oDicWords.Exists(strWord) Then
            'Add and index word to dictionary.
            oDicWords.Add Key:=strWord, Item:=1
          Else
            'Word already exists.  Uptick counter.
            oDicWords.Item(strWord) = oDicWords.Item(strWord) + 1
          End If
        End If
      Next
      For lngIndex = 0 To oDicWords.Count - 1
        If oDicWords.Items()(lngIndex) > 1 Then
          'Report on words repeated 2 or more times.
          MsgBox oDicWords.Keys()(lngIndex) & " repeated " & oDicWords.Items()(lngIndex) & " time(s)"
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Quote Originally Posted by macropod View Post
    What constitutes 'duplicate words' in this context? For example, do they have to be immediately one after the other, or can there be other, intervening, words?
    Hi Paul, I'm looking for the latter where the macro will highlight all words that have occurred more than once in the selection. Thanks!

  5. #5
    Quote Originally Posted by gmaxey View Post
    Lots of ways. Here is one using a dictionary class:
    Thank you, Greg. I'm not that adept with VBA. How easily can this be converted to highlight those repeated words? Thanks!

  6. #6
    I looked at Greg's macro and in the course of testing, I found some three-letter words that, in retrospect, should be flagged as well.

    Would it be possible to add a list of words the marco will skip if they are repeat, such as the words "and" and "the"?

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,354
    Location
    Sub ScratchMacroII()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/10/2017
    Dim oWord As Range
    Dim strWord As String
    Dim oDicWords As Object
    Dim oDicExcludes As Object
    Dim lngIndex As Long
      Set oDicWords = CreateObject("Scripting.Dictionary")
      Set oDicExcludes = CreateObject("Scripting.Dictionary")
      oDicWords.CompareMode = vbBinaryCompare 'Test and test are unique words.
      'oDicWords.CompareMode = vbTextCompare 'Test and test are the same word.
      With oDicExcludes
        .Add "and", "and"
        .Add "the", "the"
        .Add "The", "The"
      End With
      For Each oWord In Selection.Range.Words
        strWord = Trim(oWord)
        If Not oDicExcludes.Exists(strWord) Then
          If Not oDicWords.Exists(strWord) Then
            'Add and index word to dictionary.
            oDicWords.Add Key:=strWord, Item:=1
          Else
            'Word already exists.
            oWord.HighlightColorIndex = wdYellow
            'oDicWords.Item(strWord) = oDicWords.Item(strWord) + 1
          End If
        End If
      Next
    '  For lngIndex = 0 To oDicWords.Count - 1
    '    If oDicWords.Items()(lngIndex) > 1 Then
    '      'Report on words repeated 2 or more times.
    '      MsgBox oDicWords.Keys()(lngIndex) & " repeated " & oDicWords.Items()(lngIndex) & " time(s)"
    '    End If
    '  Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    This works great! Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •