简体   繁体   English

如何在发送前删除电子邮件正文中的文本?

[英]How do I remove text in the body of an email before send?

Our company puts a notice on any incoming email from an outside source to warn us to exercise caution when opening attachments or clicking on links.我们公司会对来自外部来源的任何传入电子邮件发出通知,以警告我们在打开附件或单击链接时要谨慎行事。 Warranted?保证? Yes.是的。 Annoying?恼人的? Yes.是的。 Looks somewhat unprofessional?看起来有点不专业? Maybe.也许。

I've tried several different iterations of VBA to ask if I want the message removed on send.我尝试了几种不同的 VBA 迭代来询问我是否希望在发送时删除消息。 My current code is below and it will bring up the message box, so I know that my formatting is correct and it finds the text, but it won't actually remove it.我当前的代码在下面,它会显示消息框,所以我知道我的格式是正确的并且它找到了文本,但它实际上不会删除它。

Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strBody As String

If InStr(Item.Body, "NOTICE: This email is from an external sender. Please exercise caution when opening attachments or clicking links.") > 0 Then
   If MsgBox("Do you want to remove the Notice?", vbYesNo) = vbYes Then
     strBody = Replace(Item.Body, "NOTICE: This email is from an external sender. Please exercise caution when opening attachments or clicking links.", "", vbTextCompare)

   Else
      strBody = Item.Body
   End If
End If

Item.Save
End Sub

I would like for the message box to come up and ask if the notice needs to be removed and then remove it if I click yes, but leave everything else in the email alone.我希望出现消息框并询问是否需要删除通知,如果我单击是,则将其删除,但将其他所有内容留在电子邮件中。 One caveat is that there could be multiple instances of this notice if it's a long chain with multiple replies.一个警告是,如果这条通知是一条有多个回复的长链,则可能有多个实例。 If I'm the only one on the chain then my macro will have removed any in prior replies, but if others are on the chain and don't remove theirs then I'd like my macro to do it if I reply (I realize I can't do anything about others).如果我是链上唯一的一个,那么我的宏将删除之前回复中的任何内容,但如果其他人在链上并且不删除他们的,那么如果我回复,我希望我的宏执行此操作(我意识到我不能对别人做任何事情)。

Would the code need to be any different if it's an HTML email versus plain text?如果是 HTML 电子邮件与纯文本,代码是否需要有所不同?

EDIT: Here's my current code.编辑:这是我当前的代码。 I've got it to remove the notice and it now will not delete a new email, but the send on a reply is really slow.我已经用它来删除通知,现在它不会删除新电子邮件,但发送回复真的很慢。

Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim strBody As String

If InStr(Item.HTMLBody, "LHMSE NOTICE: This email is from an external sender. Please exercise caution when opening attachments or clicking links.") > 0 Then
   If MsgBox("Do you want to remove the LHMSE Notice?", vbYesNo) = vbYes Then
     strBody = Replace(Item.HTMLBody, "LHMSE NOTICE: This email is from an external sender. Please exercise caution when opening attachments or clicking links.", "", vbTextCompare)
     Item.HTMLBody = strBody

   Else
      strBody = Item.HTMLBody
   End If
End If

Item.Save
End Sub

You never set the Item.Body property back with the new value stored in the strBody variable.您永远不会将Item.Body属性设置回存储在strBody变量中的新值。 Also keep in mind that you will wipe out the formatting since you are dealing with the plain text body rather than MailItem.HTMLBody .还要记住,您将清除格式,因为您正在处理纯文本正文而不是MailItem.HTMLBody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM