html - 用替换删除文本
问题描述
我想在每封收到的外部电子邮件的开头删除一个公司句子。
认为安全。此电子邮件来自外部来源。除非您认识发件人,否则请勿单击链接或打开附件。
它以黄色突出显示。
感谢这个社区的帮助,下面的代码可以正常工作,但是所有后续的不相关的互联网电子邮件内容也被删除,只留下原始的超链接(没有更多的横幅、图片..)。我只想删除上面显示的句子。
我已经尝试了所有组合 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
解决方案
我认为你有改变 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
推荐阅读
- c# - Visual Studio 不会自动完成或显示有关统一外部 dll 的建议
- asp.net-mvc - 模拟 VS 调试的 VS 代码设置
- python - 在opencv python中使用1通道图像作为掩码
- swift - 在 watchOS 故事板中叠加图像并自定义 x、y 位置
- asp.net - 微软。Office.interop。Excel dll异常
- firebase - 为什么 404.html 在服务工作者缓存期间会导致未捕获的错误?
- plsql - 如何为更新列的数量是动态的编写更新语句
- ckeditor - TYPO3中ckeditor下拉菜单中的字体大小
- html - 在 Spring 中从服务器端渲染 Html/js
- java - Selenium Java Vk 密钥异常