Extract eMail Data (Subject & Body) Programatically using Outlook VBA
Many automation revolves around mails; you may want to trigger some process once a mail arrives in the InBox. The following code will help you extract the subject and body content of all mails in InBox
Sub Extract_Body_Subject_From_Mails()
Dim oNS As Outlook.NameSpace
Dim oFld As Outlook.Folder
Dim oMails As Outlook.Items
Dim oMailItem As Outlook.MailItem
Dim oProp As Outlook.PropertyPage
Dim sSubject As String
Dim sBody
On Error GoTo Err_OL
Set oNS = Application.GetNamespace("MAPI")
Set oFld = oNS.GetDefaultFolder(olFolderInbox)
Set oMails = oFld.Items
For Each oMailItem In oMails
sBody = oMailItem.Body
sSubject = oMailItem.Subject 'This property corresponds to the MAPI property PR_SUBJECT. The Subject property is the default property for Outlook items.
Next
Exit Sub
Err_OL:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Err.Clear
Resume Next
End If
End Sub
The Subject property is the default property for Outlook items.
Thursday, April 17, 2008
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

I have a requirement..I would be thankful if you could let me know how to do this..
ReplyDeleteI am getting more bounced emails when i send some email newsletters to my clients..This bounced email is coming from "Mail Delivery Subsystem". it has some attachments. In that attachments, it is having my original mail.
i want to extract the original email id of the receiver for whom the emails were bounced.
Extract the "Failed Recipient" email address
ReplyDeleteWhy can't you track the incoming messages and filter the content using Regular Expressions. Hope the body of text contains "to:"
Outlook VBA Extract Bounce Mail
ReplyDeleteYou can process incoming mails using NewMailEx (http://www.outlookcode.com/codedetail.aspx?id=1410) and move the Bounce mails
Set oNS = Application.GetNamespace("MAPI")
ReplyDeleteThis line keeps coming up as an error... am I missing a reference?
Thanks for the info mentioned above. I have a requirement a shade more than that. I need to extract the contents of the emails from the last forwarded message. I receive emails that are forwaded. Though I want to ignore the Forward and store only the contents after the forward i.e the original message.
ReplyDeleteYou need to take portion of the Content right. If the message has some specific format it is doable. Can you post some sample format - how the message looks like and how you want it?
ReplyDeleteHow would I modify the code to look for a specific word in the subject, then take action on that specific message?
ReplyDeleteFinding Specific Content in Outlook Body can be achieved by Find method or by Instr function
DeleteHaving fished out the subject, how to send it into a txt via the macro?
ReplyDeleteYou can either use a FileSystemObject or Simple Open-Print and Close File statements (http://vbadud.blogspot.in/2007/04/spacing-in-text-files.html)
Delete