What event to handle for modifying the body of an email only once?

Add-in Express™ Support Service
That's what is more important than anything else

What event to handle for modifying the body of an email only once?
 
burumer




Posts: 5
Joined: 2014-08-07
When a user selects an email item, I need to look for a certain pattern in the message body. If the message body contains the string pattern I'm looking for then I need to modify it by adding some HTML tags to it. At first I thought I could use the ExplorerSelectionChange event. It turns out the event seems to fire repeatedly rather than just once only when selection changes. So my code below would keep appending "Hello" to the same message, making it look like "hello hello hello hello....". Ideally, I would like to modify each message only once. What would be the appropriate event hook I should handle? Thanks.

Private Sub adxOutlookEvents_ExplorerSelectionChange(sender As System.Object, explorer As System.Object) Handles adxOutlookEvents.ExplorerSelectionChange
Dim olExplorer As Outlook.Explorer = DirectCast(explorer, Outlook.Explorer)
Dim sel As Outlook.Selection = Nothing
Try
sel = olExplorer.Selection
Catch
End Try

If sel IsNot Nothing Then
If sel.Count > 0 Then
Dim email As Outlook.MailItem = TryCast(sel.Item(1), Outlook.MailItem)
If email IsNot Nothing Then
email.HTMLBody = "<h1>hello</h1>" & email.HTMLBody
email.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
email.Save()
Marshal.ReleaseComObject(email)
End If
End If
Marshal.ReleaseComObject(sel)
End If
End Sub
Posted 07 Aug, 2014 16:21:50 Top
Andrei Smolin


Add-in Express team


Posts: 17113
Joined: 2006-05-11
Hello,

You'll get the same issue with SelectionChange working as you expect it to work if the user selects the same message several times. This means you'll need to have a list of modified messages; use their EntryIDs.

Do you restore the original message?
Why SelectionChange and not ItemAdd or NewMailEx or some other way?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 08 Aug, 2014 01:04:31 Top