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
 
Waooooooooow That's Working Perfect, Thank you sooooooooooooooooo much, you are really great.I have no words to say thanks but yes Thank you & God bless you . :)
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I've amended the code so that it counts the number of emails that have been extracted, and then it displays that count in a message box. What happens when you run this one?

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 Cnt             As Long
    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(6)
    
    Cnt = 0
    Rw = 2
    For Each oMail In oFld.Items
        If TypeName(oMail) = "MailItem" Then
            Cnt = Cnt + 1
            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
    
    MsgBox Cnt & " emails have been extracted.", vbInformation

FinishUp:
    Application.ScreenUpdating = True
    
    Exit Sub
    
ErrHandler:
    If Err <> 0 Then
        MsgBox Err.Number & " - " & Err.Description
    End If
    
    Resume FinishUp
    
End Sub
 
Upvote 0
That's great, I'm glad it's working for you. As an alternative, you may want to use the last macro I offered, since it tells you the number of emails that was extracted.

Cheers!
 
Upvote 0
Hello, This is very helpful code you have provided, it works perfectly! May I ask how to add field "Sent" to be displayed, As I need the time stamps from certain emails and if there is a way to navigate the excel file to extract not all emails from inbox, but from certain sub-folder in the Inbox. I need the body of the email, which works perfectly with the code provided, however I have to calculate the time (therefore I need the time stamps), based on emails send in two different sub-folders Named OPEN and CLOSED. Thank you very much in advance. I am new here, so I was not sure where to write. :)
 
Upvote 0
From what I understand I should place this code in Outlook VBA. I used this code provided earlier:

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 Cnt As Long
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(6)

Cnt = 0
Rw = 2
For Each oMail In oFld.Items
If TypeName(oMail) = "MailItem" Then
Cnt = Cnt + 1
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

MsgBox Cnt & " emails have been extracted.", vbInformation


FinishUp:
Application.ScreenUpdating = True

Exit Sub

ErrHandler:
If Err <> 0 Then
MsgBox Err.Number & " - " & Err.Description
End If

Resume FinishUp

End Sub


So I need to modify it to show me "Sent" field from Outlook emails with date and hour, and show it on next column in excel and to be able to extract emails from sub folder "OPEN". Then I guess it will be another macro with the same code but extracted on another sheet for sub folder "CLOSE". Once I get the time stamps I know how to calculate the rest. I have tried to change Set oFld = oNS.GetDefaultFolder(6) with Set olFld = olFolder.Folders("OPEN") but it did not work.
 
Upvote 0
Hello, This is very helpful code you have provided, it works perfectly! May I ask how to add field "Sent" to be displayed, As I need the time stamps from certain emails and if there is a way to navigate the excel file to extract not all emails from inbox, but from certain sub-folder in the Inbox. I need the body of the email, which works perfectly with the code provided, however I have to calculate the time (therefore I need the time stamps), based on emails send in two different sub-folders Named OPEN and CLOSED. Thank you very much in advance. I am new here, so I was not sure where to write. :)

Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit
[/COLOR]
[COLOR=darkblue]Sub[/COLOR] ExtractDataFromEmails()

    [COLOR=darkblue]Dim[/COLOR] olApp           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] olNS            [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] olInbox         [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] oMail           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    
    [COLOR=darkblue]Dim[/COLOR] vSubFolders     [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] vSubFolder      [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSubject        [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sBody           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sSent           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] Cnt             [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] Rw              [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
        
    [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] ErrHandler
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    
    vSubFolders = Array("Open", "Closed")
    
    [COLOR=darkblue]Set[/COLOR] olApp = CreateObject("Outlook.Application")
    [COLOR=darkblue]Set[/COLOR] olNS = olApp.GetNamespace("MAPI")
    [COLOR=darkblue]Set[/COLOR] olInbox = olNS.GetDefaultFolder(6)
    
    Cnt = 0
    Rw = 2
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] vSubFolder [COLOR=darkblue]In[/COLOR] vSubFolders
        [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oMail [COLOR=darkblue]In[/COLOR] olInbox.Folders(vSubFolder).Items
            [COLOR=darkblue]If[/COLOR] TypeName(oMail) = "MailItem" [COLOR=darkblue]Then[/COLOR]
                Cnt = Cnt + 1
                sSubject = oMail.Subject
                sBody = oMail.Body
                sSent = oMail.SentOn
                Cells(Rw, "A").Value = s[COLOR=darkblue]Sub[/COLOR]ject
                Cells(Rw, "B").Value = sBody
                Cells(Rw, "C").Value = sSent
                Rw = Rw + 1
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] oMail
    [COLOR=darkblue]Next[/COLOR] v[COLOR=darkblue]Sub[/COLOR]Folder
    
    Columns("A:C").WrapText = [COLOR=darkblue]False[/COLOR]
    
    MsgBox Cnt & " emails have been extracted.", vbInformation

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

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,216,165
Messages
6,129,250
Members
449,497
Latest member
The Wamp

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