简体   繁体   中英

Outlook VBA macro to Ignore Spelling Errors in Selected Block of Text

When composing an email that contains a lot of programming terms I want my general spelling errors to show up with a red squiggle but it gets annoying when a lot of special words also show as errors. I can run through the spell check and tell it to 'Ignore All' for each spelling incident and the red squiggles will go away. Then as I continue composing the message the spell check continues to work on new edits.

What I'd like to do is create a VBA macro that will do this for me in the selected text or the entire message body (I don't have a preference). I'm an experienced Access VBA developer but not too familiar with the Spell Check object model in Outlook.

My idea for this came from the free Microsoft OneNote Onetastic add-in and the "No Spell Check" macro . It would be great to be able to do this in Outlook.

It seems easier (and at least possible) to clear the entire message body as opposed to selected text; this should hopefully give you some inspiration.

Note that, assuming you already have spelling errors, the message body is not immediately cleared with ShowSpellingErrors = False . Toggling the language is a quick hack, but was straightforward and simple. More ideas here .

Option Explicit
    
Sub Test()
    
    ' Add a reference to the Microsoft Word Object Library for this to compile
    Dim oDoc As Word.Document
    Dim oMail As Outlook.MailItem
    
    If TypeOf Application.ActiveInspector.CurrentItem Is Outlook.MailItem Then
        Set oMail = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If
    
    Set oDoc = oMail.GetInspector.WordEditor
    
    If Not (oDoc Is Nothing) Then
        oDoc.ShowSpellingErrors = False
        
        ' Toggling the language forces a recheck of the body, to clear red squiggles
        oDoc.Range.LanguageID = wdAfrikaans
        oDoc.Range.LanguageID = wdEnglishUS
    End If
    
End Sub

With a kick start from BigBen I was able to answer this question. I gave him the check mark but this is the function I think answers my question. (Edit: now that I see how this response is laid out I checked this answer.)

Public Sub **ClearSpellCheckSquiggles**()
' Remove the red squiggles from the current document because they may be distracting
' while composing a message with a lot special words (like code).
' New text added after this runs will still be checked and indicated by red squiggles.

    ' This assumes that you also have Word installed on your box. If so, you can
    ' access most of the Word OM from the Outlook VBE *without* referencing Word
    ' by using the ActiveInspector.WordEditor object.
    Dim oDoc As Object ' Word.Document  ' Or add a reference to the Microsoft Word Object Library for IntelliSense
    Dim oMail As Outlook.MailItem

    If TypeOf Application.ActiveInspector.CurrentItem Is Outlook.MailItem Then
        Set oMail = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If

    Set oDoc = oMail.GetInspector.WordEditor

    If Not (oDoc Is Nothing) Then

        ' Mark the current document as already spell-checked:
        oDoc.SpellingChecked = True

        ' Mark the current document as already grammar-checked (green squiggles):
        oDoc.GrammarChecked = True

    End If

End Sub

If you want to add this function to your message toolbar, open the Quick Access Toolbar when you have a message window open (not the main Outlook window). Follow the arrows in the image below.

向工具栏添加快速访问按钮

在此处输入图像描述

Thank you for your answer, this has helped me alot

Another option is to toggle the DISPLAY of checking of gram/spelling as you type options

below is just 3 lines different to your answer, 3rd line refreshes the the word 'application' (editor).

I use these 3 lines in a macro button within Word itself

oDoc.Application.Options.CheckGrammarWithSpelling = Not oDoc.Application.Options.CheckGrammarWithSpelling
oDoc.Application.Options.CheckSpellingAsYouType = Not oDoc.Application.Options.CheckSpellingAsYouType
oDoc.Application.ScreenRefresh

full macro below

Public Sub ClearSpellCheckSquiggles()
' Remove the red squiggles from the current document because they may be distracting
' while composing a message with a lot special words (like code).
' New text added after this runs will still be checked and indicated by red squiggles.

    ' This assumes that you also have Word installed on your box. If so, you can
    ' access most of the Word OM from the Outlook VBE *without* referencing Word
    ' by using the ActiveInspector.WordEditor object.
    Dim oDoc As Word.Document   ' Or add a reference to the Microsoft Word Object Library for IntelliSense
    Dim oMail As Outlook.MailItem

    If TypeOf Application.ActiveInspector.CurrentItem Is Outlook.MailItem Then
        Set oMail = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If

    Set oDoc = oMail.GetInspector.WordEditor

    If Not (oDoc Is Nothing) Then

'        ' Mark the current document as already spell-checked:
'        oDoc.SpellingChecked = True
'
'        ' Mark the current document as already grammar-checked (green squiggles):
'        oDoc.GrammarChecked = True
    oDoc.Application.Options.CheckGrammarWithSpelling = Not oDoc.Application.Options.CheckGrammarWithSpelling
    oDoc.Application.Options.CheckSpellingAsYouType = Not oDoc.Application.Options.CheckSpellingAsYouType
    oDoc.Application.ScreenRefresh
    End If

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