首页 > 解决方案 > 用替换删除文本

问题描述

我想在每封收到的外部电子邮件的开头删除一个公司句子。

认为安全。此电子邮件来自外部来源。除非您认识发件人,否则请勿单击链接或打开附件。

它以黄色突出显示。

感谢这个社区的帮助,下面的代码可以正常工作,但是所有后续的不相关的互联网电子邮件内容也被删除,只留下原始的超链接(没有更多的横幅、图片..)。我只想删除上面显示的句子。

我已经尝试了所有组合 body/htmlbody/RTFbody。

Sub RemoveExpressionFOLDER()

Dim outNS As Outlook.NameSpace
Dim outFldr As Outlook.Folder
Dim outMailItems As Outlook.Items
'Dim outMailItem As Outlook.MailItem
Dim outMailItem As Object
'Dim myinspector As Outlook.Inspector

Set outNS = Application.GetNamespace("MAPI")
Set outFldr = Application.ActiveExplorer.CurrentFolder

'Set myinspector = Application.ActiveInspector

Set outMailItems = outFldr.Items

K = outFldr.Items.Count

For i = K - 10 To K

    If outMailItems(i).Class <> olMail Then GoTo 20

    outMailItems(i).Display

    'outMailItems(i).UnRead = True
    outMailItems(i).Body = Replace(outMailItems(i).Body, "THINK SECURE. This email has come from an external source. Do not click on links or open attachments unless you recognise the sender.", "")
    'outMailItems(i).HTMLBody = Replace(outMailItems(i).HTMLBody, "THINK SECURE. This email has come from an external source. Do not click on links or open attachments unless you recognise the sender.", "")
    ' outMailItems(i).BodyFormat = olFormatHTML

    outMailItems(i).Save
    'Set myinspector = Application.ActiveInspector
    'Set outMailItems(i) = myinspector.CurrentItem

    outMailItems(i).Close olSave

20    Next i
MsgBox ("cleaned ")

Set outMailItems = Nothing
Set outFldr = Nothing
Set outNS = Nothing

End Sub

标签: htmlvbareplaceoutlook

解决方案


我认为你有改变 HTMLBody 而不是 Body 的正确想法。但是,您遇到了两个问题:

一个。您无法使用 .Save 保存并使用 .Close olSave

湾。使用 .Display 方法,Outlook 已经修改了电子邮件并删除了它的大部分 HTML/CSS,因为 Outlook 可能不知道所有的 HTML 标记和 CSS 选择器。您会看到这种效果,尤其是响应式电子邮件。

以下应该可以解决问题:

Sub RemoveExpressionFOLDER()
For Each mail In Application.ActiveExplorer.CurrentFolder.Items
    With mail
        If .Class <> olMail Then continue
        .UnRead = True
        .HTMLBody = Replace(.HTMLBody, "THINK SECURE. This email has come from an external source. Do not click on links or open attachments unless you recognise the sender.", "")
        .Close olSave
    End With
continue:
Next mail
MsgBox ("cleaned ")
End Sub

推荐阅读