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

15 comments:

  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.

    ReplyDelete
  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:"

    ReplyDelete
  3. Anonymous6:00 PM

    Outlook VBA Extract Bounce Mail

    You can process incoming mails using NewMailEx (http://www.outlookcode.com/codedetail.aspx?id=1410) and move the Bounce mails

    ReplyDelete
  4. Anonymous9:13 AM

    Set oNS = Application.GetNamespace("MAPI")

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

    ReplyDelete
    Replies
    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..

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

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

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

    ReplyDelete
    Replies
    1. Anonymous2:23 AM

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

      Delete
  8. Anonymous8:48 AM

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

    ReplyDelete
    Replies
    1. Anonymous2:22 AM

      You 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
  9. Anonymous12:45 AM

    Hi

    I would like to save the invoices(attachments) in a pdf format to a repository which is on network of my company. Whenever I receive a new email, the code should save the file automatically. First it should find the vendor name present in the name of the attachment and second the code should open the pdf attachment and find the location under the section of "Bill To" which is present inside the invoice.

    The repository should contain the main folder invoices and it should contain four sub-folders with the plant name. Under each plant name there should be sub-folders with vendor names. The received attachment should be save to respective plant folder and then to the vendor folder.

    I am little confused and worried to do this as I am new to coding. I request you to help with this problem.

    Thank you

    Best Regards
    Rajamahender Rapelly

    ReplyDelete
  10. It’s actually a great and helpful piece of information. I’m pleased that you just shared this helpful information with us.
    Please keep us informed like this. Thanks for sharing. ?? extract phone numbers from outlook

    ReplyDelete
  11. Good content and nice blog. Thanks for sharing such great information. hope you keep sharing such kind of information Yahoo Email Address Grabber

    ReplyDelete
  12. Such a nice blog. I am very grateful to read this blog. Thanks for sharing this information.
    Outlook Contact Extractor

    ReplyDelete

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