Thursday, April 17, 2008

Extract Subject and Body of eMail through Outlook VBA

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.

Exit Sub
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
Resume Next
End If
End Sub

The Subject property is the default property for Outlook items.


  1. Anonymous6:15 AM

    I have a requirement..I would be thankful if you could let me know how to do this..

    I 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.

  2. Anonymous5:26 PM

    Extract the "Failed Recipient" email address

    Why can't you track the incoming messages and filter the content using Regular Expressions. Hope the body of text contains "to:"

  3. Ravindra Sangve6:00 PM

    Outlook VBA Extract Bounce Mail

    You can process incoming mails using NewMailEx ( and move the Bounce mails

  4. Anonymous9:13 AM

    Set oNS = Application.GetNamespace("MAPI")

    This line keeps coming up as an error... am I missing a reference?

    1. Anonymous4:28 AM

      Declare application first
      Dim objApp As Outlook.Application

      then declare name space like this..
      Set oNS = objApp.GetNamespace("MAPI")

      this should solve the issue..

  5. Anonymous3:40 AM

    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.

  6. Amy Marsh6:06 AM

    You 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?

  7. How would I modify the code to look for a specific word in the subject, then take action on that specific message?

    1. Anonymous2:23 AM

      Finding Specific Content in Outlook Body can be achieved by Find method or by Instr function

  8. Anonymous8:48 AM

    Having fished out the subject, how to send it into a txt via the macro?

    1. Anonymous2:22 AM

      You can either use a FileSystemObject or Simple Open-Print and Close File statements (


Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.