简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM