简体   繁体   中英

How to IgnoreAll spelling errors for a string VBA Word?

I have a list of several spelling errors. I´m looking a way to ignore all spelling errors for certain words from that list.

The code below it seems to work but only for text formatted with Style "Test"

ActiveDocument.Styles("Test").NoProofing = True

Is there a way via VBA to make something like this?

ActiveDocument.String("MyWord").SpellingErrors.IgnoreAll 

It's possible to use Word's Find/Replace functionality to locate the word and apply the NoProofing formatting. The functionality allows searching for strings and through Replace applying formatting without affecting (deleting) the search term.

The following code snippet illustrates this. There are two variations:

  1. Using Replace All to perform the action in one step

  2. Searching and replacing one instance at a time

The first is faster, but in my tests the "squiggly red underline" denoting a spelling error is not removed from the document. The full Spell check ignores the terms, however.

The second (commented out), in my tests, does remove the error formatting, but will be slower in execution.

Sub FindToNoSpellCheck()
    Dim rng As Word.Range
    Dim findText As String
    Dim bFound As Boolean

    Set rng = ActiveDocument.content
    findText = "InsertY"
    Do
    With rng.Find
        .ClearFormatting
        .Text = findText
        .Format = True
        .MatchCase = True
        .Replacement.NoProofing = True
        .Replacement.Text = ""
        .Wrap = wdFindStop
        .Execute Replace:=wdReplaceAll
'        bFound = .Execute()
'        If bFound Then
'            rng.NoProofing = True
'            rng.Collapse wdCollapseEnd
'        End If
    End With
    Loop While bFound
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