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

如何修改Outlook宏以同时删除中英文外部邮件警告语句?

如何修改Outlook宏以同时移除中英文外部邮件警告语句?

问题背景

公司外部邮件开头会附加两段警告信息:

  • 英文:This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe.
  • 繁体中文:這是外部寄來的郵件,點擊連結或開啟附件前請停、看、聽。

现有宏仅能移除英文警告,尝试将中文语句加入InStr函数时出现乱码;修改系统“非Unicode程序语言”为简体中文后,宏直接停止运行。需要调整宏实现同时删除两段警告。

修改后的宏代码

批量清理当前文件夹邮件的宏

Option Explicit

Sub RemoveExternalEmailWarnings()
    Dim outFldr As Folder
    Dim outItems As Items
    Dim outMailItem As MailItem
    Dim i As Long
    Dim cleanCount As Long
    Dim engWarning As String
    Dim chtWarning As String
    
    ' 定义警告文本
    engWarning = "This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe."
    chtWarning = "這是外部寄來的郵件,點擊連結或開啟附件前請停、看、聽。"
    
    Set outFldr = ActiveExplorer.CurrentFolder
    Set outItems = outFldr.Items
    
    ' 从后往前遍历,避免索引错乱
    For i = outItems.Count To 1 Step -1
        If outItems(i).Class = olMail Then
            Set outMailItem = outItems(i)
            
            With outMailItem
                Dim contentModified As Boolean
                contentModified = False
                
                ' 处理HTML格式邮件
                If .BodyFormat = olFormatHTML Then
                    If InStr(.HTMLBody, engWarning) > 0 Then
                        .HTMLBody = Replace(.HTMLBody, engWarning, "")
                        contentModified = True
                    End If
                    If InStr(.HTMLBody, chtWarning) > 0 Then
                        .HTMLBody = Replace(.HTMLBody, chtWarning, "")
                        contentModified = True
                    End If
                Else
                    ' 处理纯文本格式邮件
                    If InStr(.Body, engWarning) > 0 Then
                        .Body = Replace(.Body, engWarning, "")
                        contentModified = True
                    End If
                    If InStr(.Body, chtWarning) > 0 Then
                        .Body = Replace(.Body, chtWarning, "")
                        contentModified = True
                    End If
                End If
                
                ' 仅修改后保存
                If contentModified Then
                    .Save
                    cleanCount = cleanCount + 1
                End If
            End With
        End If
    Next i
    
    MsgBox cleanCount & " 封邮件已清理完成。", vbInformation
End Sub

用于Outlook规则触发的单邮件处理宏(可选)

Sub RemoveWarningsForSingleItem(item As Outlook.MailItem)
    Dim engWarning As String
    Dim chtWarning As String
    
    engWarning = "This email originated from outside of ***** group. Do not click links or open attachments unless you recognize the sender and know the content is safe."
    chtWarning = "這是外部寄來的郵件,點擊連結或開啟附件前請停、看、聽。"
    
    With item
        Dim contentModified As Boolean
        contentModified = False
        
        If .BodyFormat = olFormatHTML Then
            If InStr(.HTMLBody, engWarning) > 0 Then
                .HTMLBody = Replace(.HTMLBody, engWarning, "")
                contentModified = True
            End If
            If InStr(.HTMLBody, chtWarning) > 0 Then
                .HTMLBody = Replace(.HTMLBody, chtWarning, "")
                contentModified = True
            End If
        Else
            If InStr(.Body, engWarning) > 0 Then
                .Body = Replace(.Body, engWarning, "")
                contentModified = True
            End If
            If InStr(.Body, chtWarning) > 0 Then
                .Body = Replace(.Body, chtWarning, "")
                contentModified = True
            End If
        End If
        
        If contentModified Then
            .Save
        End If
    End With
End Sub

关键修改说明

  1. 修复语法错误:原代码同时声明两个Sub属于非法语法,拆分出独立的批量处理和单邮件处理宏。
  2. 解决中文乱码:
    • 直接将繁体中文警告定义为字符串变量,确保VBA编辑器使用支持中文的字体(路径:工具→选项→编辑器格式,选择如微软雅黑)。
    • 新版Office VBA默认支持Unicode,无需修改系统区域设置。
  3. 优化遍历逻辑:从文件夹末尾往前遍历,避免删除邮件后索引偏移导致漏处理。
  4. 高效保存机制:仅当邮件内容确实被修改时才执行保存,减少不必要的操作。
  5. 分格式处理:针对HTML和纯文本邮件分别处理对应内容,确保替换生效。

注意事项

  • 若仍出现中文乱码,可尝试用ChrW函数拼接中文文本(每个繁体中文字符对应Unicode编码),例如chtWarning = ChrW(36828) & ChrW(26159) & ...。
  • 不要修改系统“非Unicode程序语言”,这会影响其他程序运行,并非解决VBA中文问题的正确方案。
  • 运行宏前请备份邮件,避免意外数据丢失。
  • 若要自动触发,在Outlook规则中选择“运行脚本”,指定RemoveWarningsForSingleItem宏即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:52:13