vbams-wordspelling

VBA: Writing spelling suggestions next to spelling errors in Word


In MS Word (Office for Mac 2016, version 15.31) I would like to enrich a document by marking spelling errors and by writing the first spelling suggestion next to each misspelled word: for example if the text says

I wuld like to enrich

the result I need is

I [wuld][would] like to enrich

I know that

iErrorCnt=Doc.This.SpellingErrors.Count
For J=1 to iErrorCnt
    Selection.TypeText Text:=DocThis.SpellingErrors(J)
Next J

will go through all spelling errors, and I know that

ActiveDocument.Words(1).GetSpellingSuggestions.Item(1).Name

allows to obtain the first spelling suggestion for a given word. But how do I link the misspelled word and the spelling suggestion (since the spelling suggestion is applied to words and words are indexed by integers) and how do I get them both marked in the document?


Solution

  • Try:

    Sub SpellCheck()
    Dim Rng As Range, oSuggestions As Variant
    For Each Rng In ActiveDocument.Range.SpellingErrors
      With Rng
        If .GetSpellingSuggestions.Count > 0 Then
          Set oSuggestions = .GetSpellingSuggestions
          .Text = "[" & .Text & "][" & oSuggestions(1) & "]"
        Else
          .Text = "[" & .Text & "][]"
      End If
      End With
    Next
    End Sub