You need to remove the entire block containing this message.
<div>
and </div>
are like brackets around some Html that specify that everything between <div>
and </div>
is to be treated as a block. The author of the Html might what to create a block for any of several reasons. Here the author wants to specify the appearance of the block. So <div style=xxxx>yyyy</div>
says apply style xxxx
to yyyy
.
Your question omits the trailing </div>
. If this block appears at the very end of the message, the person who coded this addition might have omitted the </div
although this is not good practice. More likely, you did not realise that the trailing <\div>
was important.
My approach would be:
- Use
InStr
to search for “This message originated …”
- Use
InStrRev
to search backwards for the <div
- Use
InStr
to search for the </div>
- Delete everything between the
div
and the <\div>
If you need the code to do this, I will send myself an email with this block so I can test the code. I do not like posting untested code.
Edit
I have written and partially tested code for the approach I would favour. I have not fully tested my code because I do not understand your approach.
As I understand it, your university adds a warning to emails received from outside the university. This happens before the email is released to you. I would expect you to use the Item Add event but you are using the Item Send event. I do not understand how this would give you the effect you seek.
I created an email containing a warning message using a gmail account and sent it to my Outlook account. The appearance of that email is:

This is not the same as your warning message, but it is close enough for testing.
I have a diagnostic routine that will output selected properties of selected emails to the Immediate Window or “all” properties of selected emails to a file. The output for the relevant part of the Html body is:
<di|
|v style='mso-element:para-border-div;border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt'>|
|<p class=MsoNormal align=center style='text-align:center;border:none;padding:0cm'><span style='font-|
|family:"inherit",serif;color:#303336;border:none windowtext 1.0pt;padding:0cm;background:aqua;mso-hi|
|ghlight:aqua;mso-fareast-language:EN-GB'>This email originated from outside the university.</span><o|
|:p></o:p></p></div>
I have deleted everything from the output except the DIV block containing the warning. If you would like more information about my diagnostic routine, I am happy to supply it.
The code to update the Html body is:
Sub RemoveWarning(ByRef ItemCrnt As MailItem)
Dim LcHtmlBody As String
Dim PosDivEnd As Long
Dim PosDivStart As Long
Dim PosMessage As Long
With ItemCrnt
' Check message contains warning
PosMessage = InStr(1, .HtmlBody, "This email originated from outside the university.")
If PosMessage = 0 Then
' No message found
Exit Sub
End If
' Find start and end div
LcHtmlBody = .HtmlBody ' Allow for "<DIV" and "<div"
PosDivStart = InStrRev(LcHtmlBody, "<div", PosMessage)
PosDivEnd = InStr(PosMessage, LcHtmlBody, "</div>")
If PosDivStart = 0 Or PosDivEnd = 0 Then
' Start div or end div or both not found
Exit Sub
End If
' Delete Div block from Html
.HtmlBody = Mid$(.HtmlBody, 1, PosDivStart - 1) & Mid$(.HtmlBody, PosDivEnd + 6)
End With
End Sub
The result of running this code is:

This has removed the entire warning, including the coloured box, but has left a gap. This code may not leave a gap with your warning message. If it does leave a gap with your message, you will probably need to delete an empty paragraph as well as the Div block. I would need to see the Html before your “<div” or after your “</div” before I could recommend how to expand my code.