title: Script focusing on wrong email
body: <p>Below is a script I cobbled together from a number of other sources. It fires each time a new email arrives through an email rule in Microsoft Outlook 2013.</p>
  <p>The script should look at the incoming email and strip out the page background.</p>
  <p>What's happening is I get the popup saying new mail has arrived, and it will strip out the background of the email that was the focus in Outlook.<br />
  If I click on an email with an html background so that it is the focus of the preview pane and then receive a new email, it will remove the background of that focused email.</p>
  <p>I want it to check the newly arrived message.</p>
  <pre class="lang-vb prettyprint-override"><code>Sub CustomMailMessageRule(Item As Outlook.MailItem)
      MsgBox &quot;Mail message arrived: &quot; &amp; Item.Subject
      Call ClearStationeryFormatting
  End Sub
      
  Sub ClearStationeryFormatting()
      On Error GoTo ClearStationeryFormatting_Error
      Dim strEmbeddedImageTag As String
      Dim strStyle As String
      Dim strReplaceThis As String
      Dim intX As Integer, intY As Integer
      Dim myMessage As Outlook.MailItem
      
      ' First, check to see if we are in preview-pane mode or message-view mode
      ' If neither, quit out
      Select Case TypeName(Outlook.Application.ActiveWindow)
          Case &quot;Explorer&quot;
              Set myMessage = ActiveExplorer.Selection.Item(1)
          Case &quot;Inspector&quot;
              Set myMessage = ActiveInspector.CurrentItem
          Case Else
              MsgBox (&quot;No message selected.&quot;)
              Exit Sub
      End Select
      
      ' Sanity check to make sure selected message is actually a mail item
      If TypeName(myMessage) &lt;&gt; &quot;MailItem&quot; Then
         MsgBox (&quot;No message selected.&quot;)
         Exit Sub
      End If
      
      ' Remove attributes from &lt;BODY&gt; tag
      intX = InStr(1, myMessage.HTMLBody, &quot;&lt;BODY&quot;, vbTextCompare)
      If intX &gt; 0 Then
          intY = InStr(intX, myMessage.HTMLBody, &quot;&gt;&quot;, vbTextCompare)
          strReplaceThis = Mid(myMessage.HTMLBody, intX, intY - intX + 1)
      End If
      
      If strReplaceThis &lt;&gt; &quot;&quot; Then
          myMessage.HTMLBody = Replace(myMessage.HTMLBody, strReplaceThis, &quot;&lt;BODY&gt;&quot;)
          strReplaceThis = &quot;&quot;
      Else
          Err.Raise vbObjectError + 7, , &quot;An unexpected error occurred searching for the BODY tag in the e-mail message.&quot;
          Exit Sub
      End If
      
      ' Find and replace &lt;STYLE&gt; tag
      intX = InStr(1, myMessage.HTMLBody, &quot;&lt;STYLE&gt;&quot;, vbTextCompare)
      If intX &gt; 0 Then
          intY = InStr(8, myMessage.HTMLBody, &quot;&lt;/STYLE&gt;&quot;, vbTextCompare)
          strReplaceThis = Mid(myMessage.HTMLBody, intX, ((intY + 8) - intX))
      End If
      
      If strReplaceThis &lt;&gt; &quot;&quot; Then
          myMessage.HTMLBody = Replace(myMessage.HTMLBody, strReplaceThis, &quot;&quot;)
      End If
      
      If InStr(1, myMessage.HTMLBody, &quot;&lt;center&gt;&lt;img id=&quot;, vbTextCompare) &gt; 0 Then
          strEmbeddedImageTag = &quot;&lt;center&gt;&lt;img id=&quot;
          '&quot;&lt;center&gt;&lt;img id=&quot;&quot;ridImg&quot;&quot; src=&quot;citbannA.gif align=bottom&gt;&lt;/center&gt;&quot;
          intX = InStr(1, myMessage.HTMLBody, strEmbeddedImageTag, vbTextCompare)
          If intX = 0 Then
              Err.Raise vbObjectError + 8, , &quot;An unexpected error occurred searching for the embedded image file name start tag in the e-mail message.&quot;
              Exit Sub
          End If
          intY = InStr(intX + Len(strEmbeddedImageTag), myMessage.HTMLBody, &quot; align=bottom&gt;&lt;/center&gt;&quot;, vbTextCompare)
          If intY = 0 Then
              Err.Raise vbObjectError + 9, , &quot;An unexpected error occurred searching for the embedded image file name end tag in the e-mail message.&quot;
              Exit Sub
          End If
          strEmbeddedImageTag = Mid(myMessage.HTMLBody, intX, intY - intX)
          intX = InStr(1, myMessage.HTMLBody, &quot;&lt;CENTER&gt;&quot;, vbTextCompare)
          intY = InStr(intX, myMessage.HTMLBody, &quot;&lt;/CENTER&gt;&quot;, vbTextCompare)
          strReplaceThis = Mid(myMessage.HTMLBody, intX, intY - intX) &amp; &quot;&lt;/CENTER&gt;&quot;
          myMessage.HTMLBody = Replace(myMessage.HTMLBody, strReplaceThis, &quot;&quot;, , , vbTextCompare)
      End If
      
      ' Finally, saved modified message
      myMessage.Save
      
      On Error GoTo 0
      Exit Sub
      
      ClearStationeryFormatting_Error:
      
      MsgBox &quot;Error &quot; &amp; Err.Number &amp; &quot; (&quot; &amp; Err.Description &amp; &quot;)&quot;
      Resume Next
  End Sub
  </code></pre>