View Single Post
 
Old 07-15-2023, 11:59 PM
puff puff is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Question [Code included] Manually iterate previous misspelling words

Hi. I'm developing a macro that when run, finds the closest misspelling word in the previously typed texts from the current cursor location. The user can then fix it (or not), and run the macro again to find the next previously misspelled word. My current code looks like this:
Code:
Sub FindPreviousMisspelledWord()
    Dim rng As Range
    Dim word As Range
    
    Set rng = Selection.Range ' Sets the range to the current selection
    
    ' Find previous misspelled word
    While rng.Start <> 0
        rng.MoveStart wdWord, -1 ' Move back one word
        
        If rng.SpellingErrors.Count > 0 Then ' Check if the word is misspelled
            Set word = rng.words(1) ' Set the range to the misspelled word
            word.Select ' Select the range
            Exit Sub
        End If
    Wend
    
    MsgBox "No misspelled words found."
End Sub
And I have 2 issues with it that I assume are interrelated:
1. After finding a misspelled word, if the word is in the middle of a sentence, it will also select the ending white space. For example, instead of selecting "liike", it selects "liike ".
2. It also selects the word that is right in front of the misspelled word, even it's correctly spelled. For example, in "the lush greeen forest", not only the code will select "greeen " but it also selects "lush "

Could someone help solve these issues? I kinda feel that the range having the ending space is the reason causing the second issue. Really appreciate any suggestions!
Reply With Quote