Extract Body Text From Outlook Email To Excel

needhelp2

Active Member
Joined
Apr 19, 2011
Messages
250
Hi, I wanna extract email body text from Outlook to excel and I am using my Gmail in outlook and all emails are available in Inbox folder moreover my subject is same in all emails but body text could be different and i got mentioned below code from search but not working. Please help me in this regard.Thankx in Advance.

Code:
sub ExtractBodySubjectFromMails()

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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
here is picture of body text of email http://ge.tt/api/1/files/7pTVDDF1/0/blob?download
blob
 
Upvote 0
First, since your code uses early binding, you'll need to make sure that you've set a reference to Outlook's object library...

Code:
VBA > Tools > References > select/check Microsoft Office Object Library > click OK

Secondly, you'll need to create an instance of Outlook..

Code:
Dim oApp as Object

Set oApp = CreateObject("Outlook.Application")

...and then you'll need to replace...

Code:
Set oNS = Application.GetNamespace("MAPI")

with

Code:
Set oNS = oApp.GetNamespace("MAPI")


Lastly, since your folder can contain an item other than 'MailItem", such as 'MeetingItem', you'll need to filter for 'MailItems' when looping through the items in your folder.

Try the following macro, which has been modified as follows...

1) it uses late binding, instead of early binding, so there's no need to set a reference

2) it filters the items in the folder for 'MailItems'

3) it places the extracted information in Column A and Column B of the active sheet, starting in the second row

Code:
Option Explicit

Sub ExtractBodySubjectFromMails()

    Dim oApp            As Object
    Dim oNS             As Object
    Dim oFld            As Object
    Dim oMail           As Variant
    
    Dim sSubject        As String
    Dim sBody           As String
    Dim Rw              As Long
        
    On Error GoTo ErrHandler
    
    Application.ScreenUpdating = False
    
    Set oApp = CreateObject("Outlook.Application")
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFld = oNS.GetDefaultFolder(olFolderInbox)
    
    Rw = 2
    For Each oMail In oFld.Items
        If TypeName(oMail) = "MailItem" Then
            sSubject = oMail.Subject
            sBody = oMail.Body
            Cells(Rw, "A").Value = sSubject
            Cells(Rw, "B").Value = sBody
            Rw = Rw + 1
        End If
    Next oMail
    
    Columns("A:B").WrapText = False

FinishUp:
    Application.ScreenUpdating = True
    
    Exit Sub
    
ErrHandler:
    If Err <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
    End If
    
    Resume FinishUp
    
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Hi thank you for your reply,

Its givinge me error when i am pressing F5 , "Variable Not Define"

All emails has same Subject called "Contact League" and are available in Folder "Inbox"
 
Last edited:
Upvote 0
Latest Status . . .

Now after selection of Miscrosoft outlook 14.0 object library, Is getting execute but i am getting nothing in my excel sheet nor it's seems busy cursor so that i could know either its progressing at back end
 
Last edited:
Upvote 0
Can you post the exact code that you're running?

Code:
Option Explicit

Sub ExtractBodySubjectFromMails()

    Dim oApp            As Object
    Dim oNS             As Object
    Dim oFld            As Object
    Dim oMail           As Variant
    
    Dim sSubject        As String
    Dim sBody           As String
    Dim Rw              As Long
        
    On Error GoTo ErrHandler
    
    Application.ScreenUpdating = False
    
    Set oApp = CreateObject("Outlook.Application")
    Set oNS = oApp.GetNamespace("MAPI")
    Set oFld = oNS.GetDefaultFolder(olFolderInbox)
    
    Rw = 2
    For Each oMail In oFld.Items
        If TypeName(oMail) = "MailItem" Then
            sSubject = oMail.Subject
            sBody = oMail.Body
            Cells(Rw, "A").Value = sSubject
            Cells(Rw, "B").Value = sBody
            Rw = Rw + 1
        End If
    Next oMail
    
    Columns("A:B").WrapText = False

FinishUp:
    Application.ScreenUpdating = True
    
    Exit Sub
    
ErrHandler:
    If Err <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
    End If
    
    Resume FinishUp
    
End Sub

blob
 
Last edited:
Upvote 0
To address your first issue, since we're using late binding, we'll need to replace...

Code:
Set oFld = oNS.GetDefaultFolder(olFolderInbox)

with

Code:
Set oFld = oNS.GetDefaultFolder(6)

Note that there will be no need to set a reference to Outlook's object library.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top