Outlook VBA宏忽略所选文本块中的拼写错误

时间:2018-04-12 18:49:52

标签: vba outlook spell-checking

在编写包含大量编程术语的电子邮件时,我希望我的一般拼写错误显示为红色波形,但是当许多特殊单词也显示为错误时,它会变得很烦人。我可以通过拼写检查并告诉它“忽略所有”'对于每个拼写事件,红色波浪线将消失。然后,当我继续编写消息时,拼写检查将继续处理新的编辑。

我想要做的是创建一个VBA宏,它将在所选文本或整个邮件正文中为我执行此操作(我没有偏好)。我是一位经验丰富的Access VBA开发人员,但对Outlook中的拼写检查对象模型不太熟悉。

我对此的想法来自免费的Microsoft OneNote Onetastic add-in"No Spell Check" macro。能够在Outlook中执行此操作会很棒。

3 个答案:

答案 0 :(得分:1)

与选定的文本相比,清除整个邮件正文似乎更容易(并且至少可能);这应该会给你一些启发。

请注意,假设您已经有拼写错误,则不会立即使用ShowSpellingErrors = False清除邮件正文。切换语言是一个快速的黑客,但简单明了。更多提示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

答案 1 :(得分:1)

从BigBen开始,我能够回答这个问题。我给了他一个复选标记,但这是我认为回答我的问题的功能。 (编辑:现在我看到这个答案是如何布局的,我检查了这个答案。)

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

如果要将此功能添加到消息工具栏,请在打开消息窗口(而不是主Outlook窗口)时打开快速访问工具栏。按照下图中的箭头。

Add quick access button to toolbar

enter image description here

答案 2 :(得分:0)

感谢您的回答,这对我有很多帮助

另一种选择是在键入选项

时切换格式/拼写检查的DISPLAY

下面只有3行与你的答案不同,第3行刷新“应用程序”(编辑)这个词。

我在Word本身的宏按钮中使用这3行

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

下面的完整宏

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