首页 文章

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

提问于
浏览
0

撰写包含许多编程术语的电子邮件时,我希望我的常规拼写错误以红色花键显示,但是当很多特殊单词也显示为错误时,这会令人讨厌。我可以进行拼写检查,并针对每次拼写事件将其告知“忽略所有内容”,红色的花体将消失。然后,当我继续撰写邮件时,拼写检查将继续在新的编辑中进行。

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

我的想法来自免费的 Microsoft OneNote Onetastic add-in“无拼写检查”宏。能够在 Outlook 中做到这一点非常好。

3 回答

  • 1

    与选择的文本相反,清除整个消息正文似乎比较容易(至少是可能的)。希望这会给您一些启发。

    请注意,假设您已经遇到拼写错误,则不会立即使用ShowSpellingErrors = False清除邮件正文。切换语言是一个快速的技巧,但又简单明了。更多想法这里

    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

    从 BigBen 开始,我就能回答这个问题。我给他打了勾,但这是我认为可以回答我的问题的功能。 (编辑:现在,我了解了此响应的布局方式,因此我检查了此 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
    

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

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

    在此处输入图片说明

  • 0

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

    另一个选项是在键入选项时切换 gram/spelling 检查的显示

    下面仅是与您的答案不同的 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
    

相关问题