0
votes

Use case: I’m using voice dictation software to make notes to myself that I paste into MS-Word. The software does a decent job but mangles some words resulting in a lot of spelling errors. Category 1 of those are basically homonyms, mostly technology terms. I built a nice VBA macro that uses find and replace, pulling the homonym and the desired correction from a spreadsheet. Works very well. Category 2 is harder to solve and comprises mostly of misspellings due to random spaces being inserted by the software into an otherwise properly spelled word. There’s no definitive pattern that I see, like always at syllable break or between double letters, but it often occurs where one or more syllables is a properly spelled word, and a different syllable is severed, and that piece alone is not a valid word. e.g. transcribes “Cat egory” versus “Category.” The correct piece can be the first or second half.

Code needs to:

  • run spell check over the ActiveDocument Range
  • find the next spelling error
  • look at the word before, check spelling of it plus the misspelled word, if spelled correctly, accept
  • else look at the word after, check spelling of it plus the misspelled word, if spelled correctly, accept
  • continue to next error

Result would be something like this:

co ding to correct spell ing err ors due to spa cing -> coding to correct spelling errors due to spacing

I know how to invoke spellcheck, cycle through the range, get the spelling suggestions, etc. but I’m struggling on how to identify the previous and next word, then run spellcheck again inside of the original spellcheck session.

Sub SpellCheck()
Dim Rng As Range, oSuggestions As Variant
For Each Rng In ActiveDocument.Range.SpellingErrors
  With Rng
    If .GetSpellingSuggestions.Count > 0 Then
      
' TBD

  End If
  End With
Next
End Sub

Any suggestions? I’m willing to accept some level of false positives or words that have to be corrected manually. Just something that fixes the above representative examples would be huge. Thanks much!

1

1 Answers

1
votes

Perhaps:

Sub SpellCheck()
Dim Rng As Range, oSuggestions As Variant
For Each Rng In ActiveDocument.Range.SpellingErrors
  With Rng
    If .Characters.First.Previous = " " Then
      .Characters.First.Previous.Delete
      .Start = .Words.First.Start
      If .SpellingErrors.Count > 0 Then
        ActiveDocument.Undo
        If .Characters.Last.Next = " " Then
          .Characters.Last.Next.Delete
          .End = .Words.Last.End
          If .SpellingErrors.Count > 0 Then ActiveDocument.Undo
        End If
      End If
    End If
  End With
Next
End Sub