0

I just suffered the same problem as described in the discussion linked to below, but with a catch: My organization has added border and highlighting to the warning banner added on all external emails.

Referenced discussion: Automatically Remove Warning in Email Body

I have developed code to strip the text out, which had to be split because the HTML source code uses different formatting for parts of the warning banner:

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

     Item.HTMLBody = Replace(Item.HTMLBody, "Attention:", "")
     Item.HTMLBody = Replace(Item.HTMLBody, "This email originated from outside the university.", "")

End Sub

This leaves behind an empty banner with a brown border and tan highlighting. All of this is prepended to the message in HTML code, but I don't know how to get VBA to search at the HTML level. I would like to modify the above to instead strip the following HTML from the message body:

<div style=3D"border:solid #9C6500 1.0pt;padding:2.0pt 2.0pt 2.0pt 2.0pt">
<p class=3D"MsoNormal" style=3D"line-height:11.0pt;background:#FFEB9C"><b><=
span style=3D"font-size:9.0pt;color:#9C6500">Attention:</span></b><span sty=
le=3D"font-size:9.0pt;color:black"> This email originated from outside the =
university.<o:p></o:p></span></p>

Can VBA edit at the HTML level, i.e., modify the source? The first line of the HTML code is what needs to go, but I am struggling to find the right command.

braX
  • 11,506
  • 5
  • 20
  • 33
JGC_WUSTL
  • 3
  • 1
  • How have you extracted this block of Html from the email? I ask because this Html is not quite valid. I suspect the "3D"s were originally "&3D;"s. The "="s at the end of lines 2, 3 and 4 should not be there. – Tony Dallimore Jan 29 '20 at 17:41

1 Answers1

0

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:

Initial appearance of email

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:

Appearance of email after amendment

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.

Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • Thanks, yes, I forgot the . I copied the text from a source view pop up in Thunderbird but I missed the last line. It look like Thunderbird appended the = as line wraps. I agree that the = should not be there, I have source view in Outlook that lacks it. The 3D stuff is also an artifact of Thunderbird. – JGC_WUSTL Jan 30 '20 at 04:15
  • Actual HTML reads:

    Attention: This email originated from outside the university.

    – JGC_WUSTL Jan 30 '20 at 04:18
  • My big problem is double quotes in the replace command, like in Item.HTMLBody = Replace(Item.HTMLBody, "
    ", "") . If I try to insert HTML code that contain " in it, this does not work. That is where I am stuck. Do I need to use something like & Chr(34) & for each "? I think I can get this if I can learn how to enter a double quote in a string. I'm missing some syntax that I never learned.
    – JGC_WUSTL Jan 30 '20 at 04:23
  • If you want a double quote in a string, you include two. For example: for "
    " use "
    ". Try it; this is a standard VBA feature.
    – Tony Dallimore Jan 30 '20 at 09:14
  • I still think it would be easier to remove the entire block in one go. – Tony Dallimore Jan 30 '20 at 09:14
  • Excellent! I knew there was some standard feature of VBA that I just didn't know. I (obviously) don't use it much. Yes, I will remove entire block, that was my goal. – JGC_WUSTL Jan 30 '20 at 18:26
  • @JGC_WUSTL I do not know if you are told an answer has been amended. I have added the code for my preferred approach so you can review it. – Tony Dallimore Jan 31 '20 at 12:47
  • Thanks! Yesterday was busy, I couldn't check. After receiving many complaint, university IT rescinded warning banner for now. May come back, so this code is still valuable. And many others have banners like this, and this shows how to remove. – JGC_WUSTL Feb 01 '20 at 16:55