Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-15-2023, 11:59 PM
puff puff is offline [Code included] Manually iterate previous misspelling words Windows 10 [Code included] Manually iterate previous misspelling words Office 2016
Advanced Beginner
[Code included] Manually iterate previous misspelling words
 
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
  #2  
Old 07-16-2023, 02:04 AM
gmayor's Avatar
gmayor gmayor is offline [Code included] Manually iterate previous misspelling words Windows 10 [Code included] Manually iterate previous misspelling words Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I may be missing something here, but why not simply run a spell check? If the spell check doesn't pick up misspelled words, then neither will a macro that needs spell check to find them.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-16-2023, 06:29 AM
vivka vivka is offline [Code included] Manually iterate previous misspelling words Windows 7 64bit [Code included] Manually iterate previous misspelling words Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, try this:


Code:
Sub FindPreviousMisspelledWord()
    
Dim rng As range
    
    Set rng = selection.range
    
    While rng.Start <> 0
        rng.Collapse 1
        rng.MoveStart wdWord, -1
        If rng.SpellingErrors.Count > 0 Then
            rng.SpellingErrors(1).Select
            Exit Sub
        End If
    Wend
    MsgBox "No misspelled words found."
End Sub
Reply With Quote
  #4  
Old 07-16-2023, 10:53 AM
puff puff is offline [Code included] Manually iterate previous misspelling words Windows 10 [Code included] Manually iterate previous misspelling words Office 2016
Advanced Beginner
[Code included] Manually iterate previous misspelling words
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
I may be missing something here, but why not simply run a spell check? If the spell check doesn't pick up misspelled words, then neither will a macro that needs spell check to find them.
Sorry for the confusion. The spell check has picked up all the misspelled words, but I want to give the users some chances to go back and fix them one by one if they want.
Reply With Quote
  #5  
Old 07-16-2023, 11:06 AM
gmaxey gmaxey is offline [Code included] Manually iterate previous misspelling words Windows 10 [Code included] Manually iterate previous misspelling words Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

vivka,


Your code is better (fixes the problems), but looking at every word regardless if spelled wrong or not, could take a long time in large documents. I mean why back up one word at at time?


Try:


Code:
Sub FindPreviousMisspelledWord()
Dim oRng As Range
  Set oRng = Selection.Range
  With oRng
    While .Start <> 0
      .Collapse 1
      .Start = ActiveDocument.Range.Start
      .MoveStart wdWord, -1
      If .SpellingErrors.Count > 0 Then
        .SpellingErrors(.SpellingErrors.Count).Select
        Exit Sub
      End If
      Wend
  End With
  MsgBox "No misspelled words found."
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 07-16-2023, 11:14 AM
puff puff is offline [Code included] Manually iterate previous misspelling words Windows 10 [Code included] Manually iterate previous misspelling words Office 2016
Advanced Beginner
[Code included] Manually iterate previous misspelling words
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
vivka,


Your code is better (fixes the problems), but looking at every word regardless if spelled wrong or not, could take a long time in large documents. I mean why back up one word at at time?


Try:


Code:
Sub FindPreviousMisspelledWord()
Dim oRng As Range
  Set oRng = Selection.Range
  With oRng
    While .Start <> 0
      .Collapse 1
      .Start = ActiveDocument.Range.Start
      .MoveStart wdWord, -1
      If .SpellingErrors.Count > 0 Then
        .SpellingErrors(.SpellingErrors.Count).Select
        Exit Sub
      End If
      Wend
  End With
  MsgBox "No misspelled words found."
lbl_Exit:
  Exit Sub
End Sub

Thank you so much! And yes, yours run faster.
Reply With Quote
Reply

Tags
loop, range, spell check



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Code included] Manually iterate previous misspelling words Using custom style instead of inserting font attributes manually in VBA code laith93 Word VBA 2 11-07-2021 12:52 AM
Error with macro to delete starting #) [code included] puff Word VBA 1 04-18-2018 03:49 PM
[Code included] Manually iterate previous misspelling words VBA to insert an image and centralize it (code included) puff Word VBA 5 01-17-2018 04:32 PM
[Code included] Manually iterate previous misspelling words Issue of 2 fixed bullet points indent setting work together (code included) puff Word VBA 5 12-17-2017 05:52 PM
Outlook 2003 - if previous meesage included in reply, spell checks all wildswing Outlook 0 07-22-2009 06:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft