You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Outlook VBA中编写代码删除邮件正文中的所有高亮文本?

在Outlook VBA中删除邮件正文中的高亮文本

必要的Outlook/Word对象说明

要实现需求,需要用到以下核心对象:

  • Outlook.Inspector:代表Outlook中打开的项目窗口,用于获取当前操作的邮件实例
  • Outlook.MailItem:代表Outlook中的单封邮件对象,通过Inspector的CurrentItem属性获取
  • Word.Document:通过MailItem.GetInspector.WordEditor获取邮件正文的Word文档对象(Outlook富文本/HTML邮件基于Word引擎渲染)

完整代码示例(早期绑定)

早期绑定需要提前引用Word对象库,优点是代码有智能提示,运行效率更高:

Sub RemoveHighlightFromEmailBody()
    Dim olInspector As Outlook.Inspector
    Dim olMailItem As Outlook.MailItem
    Dim wordDoc As Word.Document
    
    ' 获取当前活动的邮件窗口
    Set olInspector = Application.ActiveInspector
    If olInspector Is Nothing Then
        MsgBox "请先打开一封邮件!", vbExclamation
        Exit Sub
    End If
    
    ' 验证当前窗口对象为邮件项
    If olInspector.CurrentItem.Class = olMail Then
        Set olMailItem = olInspector.CurrentItem
        ' 获取邮件正文的Word编辑器实例
        Set wordDoc = olMailItem.GetInspector.WordEditor
        
        ' 执行高亮文本的查找替换(删除高亮格式)
        With wordDoc.Content.Find
            .ClearFormatting
            .Highlight = True
            .Replacement.ClearFormatting
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
        
        ' 保存修改(可根据需求注释此行)
        olMailItem.Save
        MsgBox "已完成高亮文本删除!", vbInformation
    Else
        MsgBox "当前打开的不是邮件项!", vbExclamation
    End If
    
    ' 释放对象资源
    Set wordDoc = Nothing
    Set olMailItem = Nothing
    Set olInspector = Nothing
End Sub

兼容版本代码(后期绑定)

如果不想手动引用Word对象库,可使用后期绑定,代码无需修改引用即可在不同Office版本运行:

Sub RemoveHighlightFromEmailBody_LateBinding()
    Dim olInspector As Object
    Dim olMailItem As Object
    Dim wordDoc As Object
    ' 定义Word常量的数值
    Const wdFindContinue = 1
    Const wdReplaceAll = 2
    Const olMail = 43
    
    Set olInspector = Application.ActiveInspector
    If olInspector Is Nothing Then
        MsgBox "请先打开一封邮件!", vbExclamation
        Exit Sub
    End If
    
    If olInspector.CurrentItem.Class = olMail Then
        Set olMailItem = olInspector.CurrentItem
        Set wordDoc = olMailItem.GetInspector.WordEditor
        
        With wordDoc.Content.Find
            .ClearFormatting
            .Highlight = True
            .Replacement.ClearFormatting
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
        
        olMailItem.Save
        MsgBox "已完成高亮文本删除!", vbInformation
    Else
        MsgBox "当前打开的不是邮件项!", vbExclamation
    End If
    
    Set wordDoc = Nothing
    Set olMailItem = Nothing
    Set olInspector = Nothing
End Sub

关键说明

  1. 避免使用Selection:Outlook中的Selection默认指向邮件列表的选中项,而非Word编辑区域的内容,直接操作wordDoc.Content更可靠,无需依赖用户的选中位置
  2. 引用设置(早期绑定):打开VBA编辑器后,点击【工具】→【引用】,勾选Microsoft Word xx.x Object Library(xx.x对应你的Office版本)
  3. 自动保存:代码中的olMailItem.Save会自动保存修改,若不需要自动保存可注释此行

内容的提问来源于stack exchange,提问作者WWYW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 20:05:16