How to modify an incoming email but keep it in the inbox (for further processing)

lyteck
lyteck used Ask the Experts™
on
Hello: is it possible to modify an incoming email and leave it sitting in the inbox (the email account is being monitored by another application... which will pick up the email and do further processing)

- if the item contains attachment.
       - code will append "--attachments--" to the subject
       - code will append "--attachments--" to the body

The code below is a starting point... however, I think there is a problem in that the code forwards a copy rather than replace the current copy in the inbox (for downstream processing by the application monitoring the inbox). Thanks, Lyteck

Sub ForwardEmail(Item As Outlook.MailItem)
    Dim olkForward As Outlook.MailItem, olkAttachment As Outlook.Attachment, strSubject As String, strRequester As String
        Set olkForward = Application.CreateItem(olMailItem)
        olkForward.HTMLBody = Item.HTMLBody
        If Item.Attachments.count > 0 Then
            olkForward.HTMLBody = olkForward.HTMLBody & "--attachments--"
        End If
        strSubject = Item.subject & "--attachments--"
        strRequester = item.from 'IS THIS CORRECT SYNTAX TO COPY THE ORIGINAL SENDER?
        olkForward.subject = strSubject
        olkForward.Recipients.Add strRequester
        olkForward.Recipients.ResolveAll
        olkForward.Send
    Set olkForward = Nothing
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
When any mail comes into the inbox you want to append some text to the subject and body of the received mail and leave it otherwise unaffected ... is that correct?, because if so then I believe we can help.

Chris

Author

Commented:
Yes,
- if email contains attachment, append text to subject and body
- if no attachment, leave email as is
In both cases the email sits in the inbox for further processing by other process.

Thanks Chris
Software Quality Lead Engineer
Top Expert 2011
Commented:
Add the following code to a normal module

Create a rule to run on incoming mails with attachments and run a script calling AttModPrefix

Chris
Sub AttModPrefix(mai As mailitem)
        mai.Subject = "--attachments--" & mai.Subject
        mai.body = "--attachments--" & vbCrLf & vbCrLf & mai.body
        Debug.Print "executed! at " & Now()
        mai.Save
End Sub

Open in new window

Author

Commented:
Thank you Chris. Apologies for the long delay. Lyteck

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial