问题
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. 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?
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
回答1:
You never set the Item.Body
property back with the new value stored in the strBody
variable. Also keep in mind that you will wipe out the formatting since you are dealing with the plain text body rather than MailItem.HTMLBody
.
来源:https://stackoverflow.com/questions/54654942/how-do-i-remove-text-in-the-body-of-an-email-before-send