VBA Export Outlook emails to Excel

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a Macro to export emails from Outlook to Excel. The macro works, but the email body many times is too large for a cell in Excel because it can contain data for several different entries. Each entry is divided by a line ********************* like this. Is there any possible way to adjust the macro so it divides the email into separate cells after each divider line represented by ****************? Below is the macro.

Code:
Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("LEXIS NEXIS")

i = 1

For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then
        Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
        Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
        
        i = i + 1
    End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Check if this works for you:

VBA Code:
Sub GetFromOutlook()
  Dim OutlookApp As Outlook.Application
  Dim OutlookNamespace As Namespace
  Dim Folder As MAPIFolder
  Dim OutlookMail As Variant
  Dim i As Long, j As Long
  
  Dim body_text As String
  Dim body_cell As Variant, bc As Variant
  
  Set OutlookApp = New Outlook.Application
  Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
  Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("LEXIS NEXIS")
  
  i = 1
  j = 0
  For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then
      Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
      Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
      Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
      
      body_text = OutlookMail.Body
      j = 0
      If InStr(1, body_text, "*") > 0 Then
        body_cell = Split(body_text, "*")
        For Each bc In body_cell
          If bc <> "" Then
            Range("eMail_text").Offset(i, j).Value = bc
            j = j + 1
          End If
        Next
      Else
        Range("eMail_text").Offset(i, 0).Value = body_text
      End If
      
      i = i + 1
    End If
  Next OutlookMail
  
  Set Folder = Nothing
  Set OutlookNamespace = Nothing
  Set OutlookApp = Nothing

End Sub

🤗
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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