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?
Declare application first
DeleteDim objApp As Outlook.Application
then declare name space like this..
Set oNS = objApp.GetNamespace("MAPI")
this should solve the issue..
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)
DeleteHi
ReplyDeleteI 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
It’s actually a great and helpful piece of information. I’m pleased that you just shared this helpful information with us.
ReplyDeletePlease keep us informed like this. Thanks for sharing. ?? extract phone numbers from outlook
Good content and nice blog. Thanks for sharing such great information. hope you keep sharing such kind of information Yahoo Email Address Grabber
ReplyDeleteSuch a nice blog. I am very grateful to read this blog. Thanks for sharing this information.
ReplyDeleteOutlook Contact Extractor