Detect end of new message in email conversation body

寵の児 提交于 2019-12-18 08:54:45

问题


When a message gets replied or forwared the previous thread gets appended on your new mail by placing i.e. ----Original Message---- in front of it.

Outlook does the same with tekst messages but with HTML mails it shows a line followed with From:..

Since outlook shows a line in between these messages i believe it's possible to subtract these messages using VBA. But How?

Goal: When the user sends a mail (Application_ItemSend) I want to scan the message for certain words. But I don't care if it occurs in a previous message, i only want to scan the newly typed message.

The example below will work when answering an email in outlook when it's in text (and outlook is in english!). But it will NOT work when sending a HTML email.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
  Dim EndOfMsg As Integer
  Dim myMsg as String

  EndOfMsg = InStr(Item.Body, "-----Original Message-----")
  If EndOfMsg = 0 Then 
    myMsg = Item.Body
  Else 
    myMsg = Left(Item.Body, EndOfMsg)
  End If

  MsgBox myMsg
End Sub

So i need some 'token' to split on, correct.. But what is this token? All there is between myMsg and the older msg's is a double vbCrLf. The same as when i enter a new line in my message.. no nothing at all unique!


回答1:


The answer depends on whether the user is sending HTML or rich text email. (This will usually depend on whether they are replying to/forwarding a message in HTML or RTF, unless they manually change the format.)

In either case, you need to look for a pattern that indicates the end of the message you are interested in, and stop there. Use Instr() to identify where you need to stop. Then, as you are parsing the text looking for your "certain words", keep track of how many characters you have read and stop when you get to the marker.

Rich text

Look in Item.Body for "^p^p__________________________ ^p".

That's 2 instances of VbCrLf, 46 instances of Chr(95), a space, and then another instance of VbCrLf.

HTML

In Item.Body, the token is "^p  _  ^p".

That's one VbCrLf, 2 spaces, 5 instances of Chr(95), 2 more spaces, and another VbCrLf.

In Item.HTMLBody, the token is

<DIV dir=ltr lang=en-us class=OutlookMessageHeader align=left>
<HR tabIndex=-1>

That token has a VbCrLf before it, between the two lines, and at the end.




回答2:


You could create an array of messages as below:

Dim arr() As String

arr() = Split(Item.Body, "From:")

Then loop through the messages:

Dim varElement as variant

for each varElement in arr()

next varElement



回答3:


What about changing your InStr function to EndOfMsg = InStr(1, Item.Body, "From:")?



来源:https://stackoverflow.com/questions/15768756/detect-end-of-new-message-in-email-conversation-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!