Exporting Outlook Messages to Excel


I’m writing this post primarily for Sen who in a comment to another post asked

How do i Export e-mail messages with the subject, received date & time from Outlook to Excel with the sender address?

The simplest way to carry out this is to use Outlook’s built-in export capability. Using it you can export to a .csv (comma separated values) file which you then open with Excel. However, there are a couple of drawbacks to using export. Export doesn’t allow you to pick specific messages to export so you’ll have to export an entire folder at a time. It also doesn’t allow you to limit the export to certain fields. Instead it will export everything and you’ll have to delete the columns you don’t want.

A better, but more complicated, approach is to use a macro to do the export. With a macro you can select the messages you want, export only the fields you want, and it can write directly to Excel. The code for doing this is both simple and straightforward. It creates a spreadsheet, loops though the selected messages writing the fields you want to that spreadsheet, then saves and closes the spreadsheet. This solution should work in Outlook 2007 and later.

Adding the code to Outlook.

  1. Start Outlook
  2. Press ALT+F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Click the diskette icon on the toolbar to save the changes
  8. Close the VB Editor
Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", "Export Messages to Excel")
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.VERSION, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function
 

Using the Code.

  1. With Outlook open select a folder that contains emails.
  2. Run the macro.
  3. When prompted enter a filename to save the export to. You can cancel the export by not entering anything.
  4. The macro will display a dialog-box when it’s finished. The dialog-box includes a count of the number of messages exported.

Notes.

  • This code can easily be modified to export a different set of fields. To do that, change the headings written to the spreadsheet (lines 15-17) and the message fields (lines 25-27).
  • If you don’t want the macro to prompt for a filename each time, then you can change line 7 to strFilename = “Path_and_File_Name”

Revisions.

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49  

This revision is for Sen who asked for a way to use the code to export a set list of folders each day without having to manually open and export each one individually. To accomplish that I’ve added a controller which calls the export routine for each folder in Sen’s list. All Sen needs to do is edit the ExportController subroutine by adding a line for each folder he wants to export. Each call will pass the filename and path to the spreadsheet the code will export the messages to and the path to the source Outlook folder it will export from.

In case you aren’t familiar with Outlook folder paths, here’s an explanation of how to figure them out. A folder path in Outlook is essentially the same as a folder path in the file system. The one difference being that Outlook folder paths do not include a drive letter. The path to a folder is a list of all the folders from the root to the target folder with each folder name separated from the preceding folder name by a backslash (i.e. \). Consider the following folder structure:

Mailbox - Doe, John
    - Calendar
    - Inbox
    - Tasks
Personal Folders
    + Marketing
        + Proposals
        + Reviews
    + Projects
        + Project 1
        + Project 2

The path to “Inbox” is “Mailbox – Doe, John\Inbox”.
The path to “Reviews” is “Personal Folders\Marketing\Reviews”.
The path to “Project 1″ is “Personal Folders\Projects\Project 1″.

Const MACRO_NAME = "Export Messages to Excel (Rev 1)"

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folders\Folder1"
    ExportMessagesToExcel "C:\Folder2.xlsx", "Personal Folders\Folder2"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolderPath As String)
    Dim olkMsg As Object, _
        olkFld As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkFld = OpenOutlookFolder(strFolderPath)
            If TypeName(olkFld) <> "Nothing" Then
                intVersion = GetOutlookVersion()
                Set excApp = CreateObject("Excel.Application")
                Set excWkb = excApp.Workbooks.Add()
                Set excWks = excWkb.ActiveSheet
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Subject"
                    .Cells(1, 2) = "Received"
                    .Cells(1, 3) = "Sender"
                End With
                intRow = 2
                'Write messages to spreadsheet
                For Each olkMsg In olkFld.Items
                    'Only export messages, not receipts or appointment requests, etc.
                    If olkMsg.Class = olMail Then
                        'Add a row for each field in the message you want to export
                        excWks.Cells(intRow, 1) = olkMsg.Subject
                        excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                        excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                        intRow = intRow + 1
                    End If
                Next
                Set olkMsg = Nothing
                excWkb.SaveAs strFilename
                excWkb.Close
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.VERSION, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function
 

This revision is for Canny who asked for a means of exporting to an existing spreadsheet instead of creating a new one each time, and for the exported items to be linked back to the originals in Outlook. This version implements both of these features. The first time Canny runs the export the code will create the spreadsheet if it doesn’t exist. On each subsequent export it will either overwrite the first sheet in the workbook or add a sheet. The behavior is controlled by changing the value of EXPORT_NEWSHEET (True = add a new sheet each time, False = overwrite the first sheet in the book each time).

If the hyperlinks from the items in the spreadsheet to the original items in Outlook don’t work, then it’s because you don’t have the Outlook protocol handler installed. Outlook 2007 and later doesn’t automatically install the Outlook protocol handler (which allows linking to Outlook items and folders) like earlier versions of Outlook did. You have to edit the registry and add an entry to enable it. Here’s a link to a page with details and a .reg file you can download to speed up the process. Pay attention to the instructions on the page about editing the .reg file. Also, if you have Outlook 2010 you’ll have to make another edit, changing the references to Office12 to Office14.

'On the next line change the value to True if you want each export to go on a new sheet.
Const EXPORT_NEWSHEET = False
Const MACRO_NAME = "Export Messages to Excel (Rev 2)"

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folders\Folder1"
    ExportMessagesToExcel "C:\Folder2.xlsx", "Personal Folders\Folder2"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolderPath As String)
    Dim olkMsg As Object, _
        olkFld As Outlook.MAPIFolder, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVer As Integer
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkFld = OpenOutlookFolder(strFolderPath)
            If TypeName(olkFld) <> "Nothing" Then
                intVer = GetOutlookVersion()
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set excApp = CreateObject("Excel.Application")
                If objFSO.FileExists(strFilename) Then
                    Set excWkb = excApp.Workbooks.Open(strFilename)
                    If EXPORT_NEWSHEET Then
                        Set excWks = excWkb.Worksheets.Add()
                        excWks.Name = Format(Date, "m-dd-yy") 
                    Else
                        Set excWks = excWkb.Worksheets(1)
                    End If
                Else
                    Set excWkb = excApp.Workbooks.Add()
                    Set excWks = excWkb.Worksheets(1)
                End If
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Subject"
                    .Cells(1, 2) = "Received"
                    .Cells(1, 3) = "Sender"
                End With
                intRow = 2
                'Write messages to spreadsheet
                For Each olkMsg In olkFld.Items
                    If olkMsg.Class = olMail Then
                        'Add a row for each field in the message you want to export
                        excWks.Cells(intRow, 1) = olkMsg.Subject
                        excWks.Hyperlinks.Add Anchor:=excWks.Range("A" & intRow), Address:="outlook:" & olkMsg.EntryID, TextToDisplay:=IIf(olkMsg.Subject = "", "Blank", olkMsg.Subject)
                        excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                        excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVer)
                        intRow = intRow + 1
                    End If
                Next
                Set olkMsg = Nothing
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    If objFSO.FileExists(strFilename) Then
        excWkb.Save
    Else
        excWkb.SaveAs strFilename
    End If
    excWkb.Close True
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision is for Dan who asked “Is there a way using the original version of your code to include emails in subfolders of the folder selected?” Of course the answer is “yes”. In order to handle sub-folders I had to move code from the main routine (i.e. ExportMessagesToExcel) to a new subroutine called ProcessFolder and add a recursive call (i.e. have that routine call itself once for each subfolder). This allows the code to process the currently selected folder and all its subfolders. Other than that this version is identical to the original version.

Const MACRO_NAME = "Export Messages to Excel (Rev 3)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    intMessages As Integer

Sub ExportMessagesToExcel()
    Dim strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        ProcessFolder Application.ActiveExplorer.CurrentFolder
        excWkb.SaveAs strFilename
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intMessages & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder, intRow As Integer
    intRow = excWks.UsedRange.Rows.Count
    intRow = intRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
            intRow = intRow + 1
            intMessages = intMessages + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkssub = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.VERSION, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Kris Fong and chirag both of whom asked for a modification to export based on a date range. To accomplish that I’ve added a second InputBox that takes a date range in the form mm/dd/yyyy to mm/dd/yyyy. The code tacks on “12:00am” to the starting date and “11:59pm” to the ending date, which it then uses to filter the folder. The code in this revision is based on the code in the original post, so it doesn’t handle the modifications from the other revisions.

Const MACRO_NAME = "Export Messages to Excel (Rev 4)"

Sub ExportMessagesToExcel()
    Dim olkLst As Object, _
        olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        strDateRange As String, _
        arrTemp As Variant, _
        datStart As Date, _
        datEnd As Date
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
        arrTemp = Split(strDateRange, "to")
        datStart = IIF(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
        datEnd = IIF(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        intRow = 2
        'Write messages to spreadsheet
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set olkLst = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Christy who asked for the ability to export the “To” and “Attachment” fields (i.e. the names of the attachments not the actual attachments themselves) in addition to the fields the script is already exporting.

Const MACRO_NAME = "Export Messages to Excel (Rev 5)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        olkAtt As Outlook.Attachment, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        strAtt As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "To"
            .Cells(1, 5) = "Attachments"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                excWks.Cells(intRow, 4) = olkMsg.To
                strAtt = ""
                For Each olkAtt In olkMsg.Attachments
                    If Not IsHiddenAttachment(olkAtt) Then
                        strAtt = strAtt & olkAtt.FileName & ", "
                    End If
                Next
                If strAtt <> "" Then
                    strAtt = Left(strAtt, Len(strAtt) - 2)
                End If
                excWks.Cells(intRow, 5) = strAtt
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for chirag who asked for two modifications: working off of a date range (the same as in revision 4) and saving the attachments of the items that fall into the date range to a folder on the hard drive. Be sure to edit the EXPORT_FOLDER constant. It contains the path to the folder the attachments will be saved to.

Const MACRO_NAME = "Export Messages to Excel (Rev 6)"

Sub ExportMessagesToExcel()
    'Edit the path on the next line.  This is the folder the code will save the attachments to.  Make sure that the path ends with a \
    Const EXPORT_FOLDER = "c:\SomeFolder\"
    Dim olkLst As Object, _
        olkMsg As Object, _
        olkAtt As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        strDateRange As String, _
        arrTemp As Variant, _
        datStart As Date, _
        datEnd As Date
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
        arrTemp = Split(strDateRange, "to")
        datStart = IIF(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
        datEnd = IIF(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        intRow = 2
        'Write messages to spreadsheet
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                For Each olkAtt In olkMsg.Attachments
                    If Not IsHiddenAttachment(olkAtt) Then olkAtt.SaveAsFile EXPORT_FOLDER & olkAtt.FileName
                Next
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set olkLst = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This rev is for Nilesh who asked for a version which appends data to an existing sheet instead of creating a new sheet or overwriting an existing sheet. The code will always write to the same workbook and sheet, and will append the exported data to the bottom of the sheet. Other than that this rev is identical to the code in the original post.

'On the next line edit the path to the spreadsheet you want to export to
Const WORKBOOK_PATH = "c:\Users\David\Documents\Sample.xlsx"
'On the next line edit the name of the sheet you want to export to
Const SHEET_NAME = "Sheet1"
Const MACRO_NAME = "Export Messages to Excel (Rev 7)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intExp As Integer, _
        intVersion As Integer
    intVersion = GetOutlookVersion()
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(WORKBOOK_PATH)
    Set excWks = excWkb.Worksheets(SHEET_NAME)
    intRow = excWks.UsedRange.Rows.Count + 1
    'Write messages to spreadsheet
    For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
            intRow = intRow + 1
            intExp = intExp + 1
        End If
    Next
    Set olkMsg = Nothing
    excWkb.Close True
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intExp & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.VERSION, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Alex who has a special situation. Alex receives messages with an HTML table in the body. In addition to the subject, sender, etc., Alex needs to export the table into the spreadsheet. To accomplish this I’ve added a function that uses regular expressions to parse the table out of the message. I then push the table into the clipboard and paste it into the spreadsheet. The code that copies the HTML to the clipboard is not mine. It comes from this Microsoft support article.

'On the next line edit the path to the spreadsheet you want to export to
Const WORKBOOK_PATH = "c:\Users\David\Documents\Sample.xlsx"
'On the next line edit the name of the sheet you want to export to
Const SHEET_NAME = "Sheet1"
Const MACRO_NAME = "Export Messages to Excel (Rev 8)"

Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) _
   As Long
Private Declare Function GlobalAlloc Lib "kernel32" ( _
   ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function SetClipboardData Lib "user32" ( _
   ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias _
   "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
   As Long
Private Declare Function GlobalUnlock Lib "kernel32" ( _
   ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
   pDest As Any, pSource As Any, ByVal cbLength As Long)
Private Declare Function GetClipboardData Lib "user32" ( _
   ByVal wFormat As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
   ByVal lpData As Long) As Long

Private Const m_sDescription = _
                  "Version:1.0" & vbCrLf & _
                  "StartHTML:aaaaaaaaaa" & vbCrLf & _
                  "EndHTML:bbbbbbbbbb" & vbCrLf & _
                  "StartFragment:cccccccccc" & vbCrLf & _
                  "EndFragment:dddddddddd" & vbCrLf
                  
Private m_cfHTMLClipFormat As Long

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intExp As Integer, _
        intVersion As Integer
    intVersion = GetOutlookVersion()
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(WORKBOOK_PATH)
    Set excWks = excWkb.Worksheets(SHEET_NAME)
    excApp.Visible = True
    intRow = excWks.UsedRange.Rows.Count + 1
    'Write messages to spreadsheet
    For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
            PutHTMLClipboard GetTable(olkMsg.HTMLBody)
            excWks.Range("D" & intRow).Select
            excWks.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=False
            intRow = intRow + 1
            intExp = intExp + 1
        End If
    Next
    Set olkMsg = Nothing
    excWkb.Close True
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intExp & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function GetTable(strBody As String) As String
    Dim objRegEx As Object, colMatches As Object, varMatch As Variant
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Global = True
        .Pattern = "<table.*?>(.*?)</table>"
        Set colMatches = .Execute(strBody)
    End With
    For Each varMatch In colMatches
        GetTable = varMatch
    Next
    Set objRegEx = Nothing
    Set colMatches = Nothing
End Function

Function RegisterCF() As Long
   'Register the HTML clipboard format
   If (m_cfHTMLClipFormat = 0) Then
      m_cfHTMLClipFormat = RegisterClipboardFormat("HTML Format")
   End If
   RegisterCF = m_cfHTMLClipFormat
   
End Function

Public Sub PutHTMLClipboard(sHtmlFragment As String, _
   Optional sContextStart As String = "<HTML><BODY>", _
   Optional sContextEnd As String = "</BODY></HTML>")
   
   Dim sData As String
   
   If RegisterCF = 0 Then Exit Sub
   
   'Add the starting and ending tags for the HTML fragment
   sContextStart = sContextStart & "<!--StartFragment -->"
   sContextEnd = "<!--EndFragment -->" & sContextEnd
   
   'Build the HTML given the description, the fragment and the context.
   'And, replace the offset place holders in the description with values
   'for the offsets of StartHMTL, EndHTML, StartFragment and EndFragment.
   sData = m_sDescription & sContextStart & sHtmlFragment & sContextEnd
   sData = Replace(sData, "aaaaaaaaaa", _
                   Format(Len(m_sDescription), "0000000000"))
   sData = Replace(sData, "bbbbbbbbbb", Format(Len(sData), "0000000000"))
   sData = Replace(sData, "cccccccccc", Format(Len(m_sDescription & _
                   sContextStart), "0000000000"))
   sData = Replace(sData, "dddddddddd", Format(Len(m_sDescription & _
                   sContextStart & sHtmlFragment), "0000000000"))

   'Add the HTML code to the clipboard
   If CBool(OpenClipboard(0)) Then
   
      Dim hMemHandle As Long, lpData As Long
      
      hMemHandle = GlobalAlloc(0, Len(sData) + 10)
      
      If CBool(hMemHandle) Then
               
         lpData = GlobalLock(hMemHandle)
         If lpData <> 0 Then
            
            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle
                        
         End If
      
      End If
   
      Call CloseClipboard
   End If

End Sub

I created this revision for Chris who posted a comment asking for a way to

  • Create a new workbook each time the script is run
  • Extract the entire mailbox to the workbook
  • Create a new worksheet for each store (in Outlook a store is an either an Exchange mailbox or PST file)
  • Dump all folders in the store to the same sheet

With everything in a given store going to the same worksheet I thought it might be handy to add a column that shows which folder the item is in. Hopefully that will save Chris some confusion. The rest of the code remains unchanged.

Const MACRO_NAME = "Export Messages to Excel (Rev 9)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    intMessages As Integer, _
    lngRow As Long

Sub ExportMessagesToExcel()
    Dim strFilename As String, olkSto As Outlook.Store
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        For Each olkSto In Session.Stores
            Set excWks = excWkb.Worksheets.Add()
            excWks.Name = olkSto.DisplayName
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "Folder"
                .Cells(1, 2) = "Subject"
                .Cells(1, 3) = "Received"
                .Cells(1, 4) = "Sender"
            End With
            lngRow = 2
            ProcessFolder olkSto.GetRootFolder()
        Next
        excWkb.SaveAs strFilename
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intMessages & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(lngRow, 1) = olkFld.Name
            excWks.Cells(lngRow, 2) = olkMsg.Subject
            excWks.Cells(lngRow, 3) = olkMsg.ReceivedTime
            excWks.Cells(lngRow, 4) = GetSMTPAddress(olkMsg, intVersion)
            lngRow = lngRow + 1
            intMessages = intMessages + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkSub = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Christian who asked if I could modify the code in revision 4 to use the current date in lieu of a date range. Revision 4 prompts for starting and ending dates and then extracts all the messages in the current folder that arrived between those two dates. Instead of prompting for a date range this revision gets today’s date and uses it.

Const MACRO_NAME = "Export Messages to Excel (Rev 10)"

Sub ExportMessagesToExcel()
    Dim olkLst As Object, _
        olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        datStart As Date, _
        datEnd As Date
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        datStart = Date & " 12:00am"
        datEnd = Date & " 11:59pm"
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        intRow = 2
        'Write messages to spreadsheet
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set olkLst = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for nickyzzz who asked for a solution that exports voting responses. Nickyzzz explained that she routinely sends messages with a document attached asking the recipient to review the document and accept/reject it. She’s using Outlook’s voting buttons to handle the response portion. The code here is that from the original post with a “Vote” column added. For each message the code places the value of the voting button the recipient clicked (i.e. VotingResponse) in that column.

Const MACRO_NAME = "Export Messages to Excel (Rev 11)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "Vote"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                excWks.Cells(intRow, 4) = olkMsg.VotingResponse
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for Gur Pollante who asked for a version that melds revisions 1 and 7. What Gur needs is a version that automates exporting the same folder every day (like revision 1) while appending the data to an existing spreadsheet (like revision 7). He also wants to move the exported message from the source folder to a history folder once the export is complete.

Const MACRO_NAME = "Export Messages to Excel (Rev 12)"

'On the next line edit the path as needed
Const HIST_PATH = "Personal Folder - HISTORY"

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folder\Daily"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolderPath As String)
    Dim olkMsg As Object, _
        olkFld As Object, _
        olkHst As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        intCnt As Integer
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkFld = OpenOutlookFolder(strFolderPath)
            Set olkHst = OpenOutlookFolder(HIST_PATH)
            If TypeName(olkFld) <> "Nothing" Then
                intVersion = GetOutlookVersion()
                Set excApp = CreateObject("Excel.Application")
                Set excWkb = excApp.Workbooks.Open(strFilename)
                Set excWks = excWkb.ActiveSheet
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Subject"
                    .Cells(1, 2) = "From"
                    .Cells(1, 3) = "Date"
                End With
                intRow = excWks.UsedRange.Rows.Count + 1
                'Write messages to spreadsheet
                For intCnt = olkFld.Items.Count To 1 Step -1
                    Set olkMsg = olkFld.Items(intCnt)
                    'Only export messages, not receipts or appointment requests, etc.
                    If olkMsg.Class = olMail Then
                        'Add a row for each field in the message you want to export
                        excWks.Cells(intRow, 1) = olkMsg.Subject
                        excWks.Cells(intRow, 2) = GetSMTPAddress(olkMsg, intVersion)
                        excWks.Cells(intRow, 3) = olkMsg.ReceivedTime
                        intRow = intRow + 1
                        olkMsg.Move olkHst
                    End If
                Next
                Set olkMsg = Nothing
                excWkb.Close True
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is also for Gur Pollante who asked if I could make to modifications to Rev 12. First, he wants to append new data beginning at the first row where the cell in column A is blank instead of the first unused row. This allows him to add a formula in another column, something that would change the first unused row. Second, he’d like to schedule running the script, so it needs to be converted from VBA (which runs inside of an Office product) to VBScript (which runs outside of Office products). With the script converted to VBScript Gur can schedule it to run using Windows’ built in task scheduler.

Instructions.

  1. Start Notepad.
  2. Copy the code from the code snippet box and paste it into Notepad.
  3. Edit the code as needed.
  4. Save the script. You can name it anything you want, just be sure that the file extension is .vbs
  5. Run the script by double-clicking on it.
Const MACRO_NAME = "Export Messages to Excel (Rev 13)"
Const olMail = 43

'On the next line edit the path as needed
Const HIST_PATH = "Personal Folder - HISTORY"

Dim olkApp, olkSes, excApp, excWkb, excWks

'Add as many exports as you need. Each export is controlled by a line like the following
'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folder\Daily", "Data"
MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME


Sub ExportMessagesToExcel(strFilename, strFolderPath, strSheet)
    Dim olkMsg, olkFld, olkHst, ntRow, intVersion, intCnt
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkApp = CreateObject("Outlook.Application")
            Set olkSes = olkApp.GetNamespace("MAPI")
            olkSes.Logon olkApp.DefaultProfileName
            Set olkFld = OpenOutlookFolder(strFolderPath)
            Set olkHst = OpenOutlookFolder(HIST_PATH)
            If TypeName(olkFld) <> "Nothing" Then
                intVersion = GetOutlookVersion()
                Set excApp = CreateObject("Excel.Application")
                Set excWkb = excApp.Workbooks.Open(strFilename)
                Set excWks = excWkb.Sheets(strSheet)
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Subject"
                    .Cells(1, 2) = "From"
                    .Cells(1, 3) = "Date"
                End With
                intRow = GetFirstBlankRow()
                'Write messages to spreadsheet
                For intCnt = olkFld.Items.Count To 1 Step -1
                    Set olkMsg = olkFld.Items(intCnt)
                    'Only export messages, not receipts or appointment requests, etc.
                    If olkMsg.Class = olMail Then
                        'Add a row for each field in the message you want to export
                        excWks.Cells(intRow, 1) = olkMsg.Subject
                        excWks.Cells(intRow, 2) = GetSMTPAddress(olkMsg, intVersion)
                        excWks.Cells(intRow, 3) = olkMsg.ReceivedTime
                        intRow = intRow + 1
                        olkMsg.Move olkHst
                    End If
                Next
                Set olkMsg = Nothing
                excWkb.Close True
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    olkSes.Logoff
    Set olkSes = Nothing
    Set olkApp = Nothing
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Function OpenOutlookFolder(strFolderPath)
    Dim arrFolders, varFolder, bolBeyondRoot
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = olkSes.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Private Function GetSMTPAddress(Item, intOutlookVersion)
    Dim olkSnd, olkEnt
    On Error Resume Next
    Select Case intOutlookVersion
        Case 1-13
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion()
    Dim arrVer
    arrVer = Split(olkApp.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg)
    Dim olkPA
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function GetFirstBlankRow()
    Dim intCnt
    intCnt = 1
    Do Until excWks.Cells(intCnt, 1) = ""
        intCnt = intCnt + 1
    Loop
    GetFirstBlankRow = intCnt
End Function

I put this revision together for Leslie Roman who receives messages with information that she needs to parse out of the message body. To accomplish that I’ve modified the code to look for certain keywords in the body and extract everything on the line the keyword is on from the end of the keyword to the end of the line.

Const MACRO_NAME = "Export Messages to Excel (Rev 14)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strBuffer As String, _
        strFilename As String, _
        strTemp As String, _
        arrLines As Variant, _
        varLine As Variant, _
        bolComments As Boolean
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "Name"
            .Cells(1, 5) = "Order#"
            .Cells(1, 6) = "Email"
            .Cells(1, 7) = "Testimonial"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                strBuffer = ""
                bolComments = False
                arrLines = Split(olkMsg.Body, vbCrLf)
                For Each varLine In arrLines
                    strTemp = Trim(varLine)
                    If bolComments Then
                        strBuffer = strBuffer & strTemp
                    Else
                        If Left(strTemp, 6) = "Name: " Then
                            excWks.Cells(intRow, 4) = Mid(strTemp, 7)
                        Else
                            If Left(strTemp, 10) = "Order Id: " Then
                                excWks.Cells(intRow, 5) = Mid(strTemp, 11)
                            Else
                                If Left(strTemp, 9) = "Order #: " Then
                                    excWks.Cells(intRow, 5) = Mid(strTemp, 9)
                                    bolComments = True
                                Else
                                    If Left(strTemp, 7) = "Email: " Then
                                        excWks.Cells(intRow, 6) = Mid(strTemp, 8)
                                    Else
                                        If Left(strTemp, 12) = "Testimonial:" Then
                                            bolComments = True
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                Next
                excWks.Cells(intRow, 7) = strBuffer
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Santhosh Mateti who asked

1) How export the selected mail items which are in same folder? Example : Subject contain “Down”.
2) How to append the exporting mail items to exiting Excel ?

This version is essentially the same as revision 2 with the exception that it doesn’t link to the original message and only processes messages that have the word “down” in the subject line.

'On the next line change the value to True if you want each export to go on a new sheet.
Const EXPORT_NEWSHEET = False
Const MACRO_NAME = "Export Messages to Excel (Rev 15)"

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folders\Folder1"
    ExportMessagesToExcel "C:\Folder2.xlsx", "Personal Folders\Folder2"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolderPath As String)
    Dim olkMsg As Object, _
        olkFld As Outlook.MAPIFolder, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVer As Integer
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkFld = OpenOutlookFolder(strFolderPath)
            If TypeName(olkFld) <> "Nothing" Then
                intVer = GetOutlookVersion()
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set excApp = CreateObject("Excel.Application")
                If objFSO.FileExists(strFilename) Then
                    Set excWkb = excApp.Workbooks.Open(strFilename)
                    If EXPORT_NEWSHEET Then
                        Set excWks = excWkb.Worksheets.Add()
                        excWks.Name = Format(Date, "m-dd-yy") 
                    Else
                        Set excWks = excWkb.Worksheets(1)
                    End If
                Else
                    Set excWkb = excApp.Workbooks.Add()
                    Set excWks = excWkb.Worksheets(1)
                End If
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Subject"
                    .Cells(1, 2) = "Received"
                    .Cells(1, 3) = "Sender"
                End With
                intRow = 2
                'Write messages to spreadsheet
                For Each olkMsg In olkFld.Items
                    If olkMsg.Class = olMail Then
                        If InStr(1, LCase(olkMsg.Subject), "down") then
                            'Add a row for each field in the message you want to export
                            excWks.Cells(intRow, 1) = olkMsg.Subject
                            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVer)
                            intRow = intRow + 1
                        End If
                    End If
                Next
                Set olkMsg = Nothing
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    If objFSO.FileExists(strFilename) Then
        excWkb.Save
    Else
        excWkb.SaveAs strFilename
    End If
    excWkb.Close True
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision is for Nicolas who asked for a way to merge revisions 1 and 3 in such a way that he can pass the process a path to the Outlook folder he wants to export and have the script export that folder and all its sub-folders. This revision accomplishes that. Nicolas will use the instrucitons from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 16)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    intMessages As Integer

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folders\Folder1"
    ExportMessagesToExcel "C:\Folder2.xlsx", "Personal Folders\Folder2"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolder As String)
    Dim olkFolder As Outlook.MAPIFolder
    If strFilename <> "" Then
        Set olkFolder = OpenOutlookFolder(strFolder)
        If TypeName(olkFolder) = "Nothing" Then
            MsgBox "Could not find the folder '" & strFolder & "'.  Processing aborted.", vbCritical + vbOKOnly + vbSystemModal, MACRO_NAME
        Else
            intMessages = 0
            intVersion = GetOutlookVersion()
            Set excApp = CreateObject("Excel.Application")
            Set excWkb = excApp.Workbooks.Add
            Set excWks = excWkb.Worksheets(1)
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "Subject"
                .Cells(1, 2) = "Received"
                .Cells(1, 3) = "Sender"
            End With
            ProcessFolder olkFolder
            excWkb.SaveAs strFilename
            MsgBox "Process complete.  A total of " & intMessages & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
        End If
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    Set olkFolder = Nothing
End Sub

Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder, intRow As Integer
    intRow = excWks.UsedRange.Rows.Count
    intRow = intRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
            intRow = intRow + 1
            intMessages = intMessages + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkssub = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision is for Christopher. See his comment below, it’s the one dated 2012/12/11 at 12:55 pm., for details.

'On the next line change the value to True if you want each export to go on a new sheet.
Const EXPORT_NEWSHEET = False
'On the next line edit the list of categories you want to export for. The list must be comma-separated values.
Const EXPORT_CATS = "Accepted,Pending,Declined"
'On the next line change the value to the number of characters you want to pull from the beginning of the message.
Const CHAR_LIMIT = 70
Const MACRO_NAME = "Export Messages to Excel (Rev 17)"

Sub ExportController()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", "Personal Folders\Folder1"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, strFolderPath As String)
    Dim olkMsg As Object, _
        olkFld As Outlook.MAPIFolder, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intExp As Integer, _
        intRow As Integer, _
        intVer As Integer, _
        arrCat As Variant, _
        arrExp As Variant, _
        varExp As Variant, _
        bolExp As Boolean, _
        strTmp As String
    If strFilename <> "" Then
        If strFolderPath <> "" Then
            Set olkFld = OpenOutlookFolder(strFolderPath)
            If TypeName(olkFld) <> "Nothing" Then
                intVer = GetOutlookVersion()
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set excApp = CreateObject("Excel.Application")
                If objFSO.FileExists(strFilename) Then
                    Set excWkb = excApp.Workbooks.Open(strFilename)
                    If EXPORT_NEWSHEET Then
                        Set excWks = excWkb.Worksheets.Add()
                        excWks.Name = Format(Date, "m-dd-yy")
                    Else
                        Set excWks = excWkb.Worksheets(1)
                    End If
                Else
                    Set excWkb = excApp.Workbooks.Add()
                    Set excWks = excWkb.Worksheets(1)
                End If
                'Write Excel Column Headers
                With excWks
                    .Cells(1, 1) = "Date"
                    .Cells(1, 2) = "MyName"
                    .Cells(1, 3) = "Company"
                    .Cells(1, 4) = "First"
                    .Cells(1, 5) = "Last"
                    .Cells(1, 6) = "Title"
                    .Cells(1, 7) = "Email"
                    .Cells(1, 8) = "Phone"
                    .Cells(1, 9) = "Industry"
                    .Cells(1, 10) = "2ndContact"
                    .Cells(1, 11) = "Method"
                    .Cells(1, 12) = "Attempt"
                    .Cells(1, 13) = "Result"
                    .Cells(1, 14) = "Message"
                    .Cells(1, 15) = "Notes"
                    .Cells(1, 16) = "Notes 2"
                    .Cells(1, 17) = "Co Size"
                    .Cells(1, 18) = "ID"
                End With
                arrExp = Split(EXPORT_CATS, ",")
                'Write messages to spreadsheet
                For Each olkMsg In olkFld.Items
                    If olkMsg.Class = olMail Then
                        For intRow = 2 To excWks.UsedRange.Rows.Count
                            If excWks.Cells(intRow, 18) = olkMsg.EntryID Then
                                bolFound = True
                                Exit For
                            End If
                        Next
                        If Not bolFound Then intRow = excWks.UsedRange.Rows.Count + 1
                        bolExp = False
                        arrCat = Split(olkMsg.Categories, ",")
                        For Each varExp In arrExp
                            For intExp = LBound(arrCat) To UBound(arrCat)
                                If varExp = arrCat(intExp) Then
                                    bolExp = True
                                    Exit For
                                End If
                            Next
                            If bolExp Then Exit For
                        Next
                        If bolExp Then
                            'Add a row for each field in the message you want to export
                            excWks.Cells(intRow, 1) = Date
                            'On the next line replace John Doe Inc with the name of your company
                            strTmp = Replace(olkMsg.Subject, " call with John Doe Inc", "")
                            If Left(strTmp, 4) = "RE: " or Left(strTmp, 4) = "FW: " Then strTmp = Mid(strTmp, 5)
                            excWks.Cells(intRow, 3) = strTmp
                            excWks.Cells(intRow, 13) = olkMsg.Categories
                            excWks.Cells(intRow, 14) = Left(olkMsg.Body, CHAR_LIMIT)
                            excWks.Cells(intRow, 18) = olkMsg.EntryID
                            intRow = intRow + 1
                        End If
                    End If
                Next
                Set olkMsg = Nothing
            Else
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            End If
        Else
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    If objFSO.FileExists(strFilename) Then
        excWkb.Save
    Else
        excWkb.SaveAs strFilename
    End If
    excWkb.Close True
    Set olkMsg = Nothing
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision is for Andre. See his comment below, it’s the one dated 2012/12/26 at 2:11 pm., for complete details. In short, Andre receives automated emails letting him know the current storage capacity of servers he works with. Andre needs to extract the message time and a line of information from the body of those messages to an Excel spreadsheet. The code I’ve used to accomplish this is a slight variation of this post’s original code. Andre will use the instructions from the original post to add this code to Outlook and to run it.

Const MACRO_NAME = "Export Messages to Excel (Rev 18)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCnt As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        objFSO As Object, _
        bolExists As Boolean
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", "Export Messages to Excel")
    If strFilename <> "" Then
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set excApp = CreateObject("Excel.Application")
        bolExists = objFSO.FileExists(strFilename)
        If bolExists Then
            Set excWkb = excApp.Workbooks.Open(strFilename)
        Else
            Set excWkb = excApp.Workbooks.Add()
        End If
        'On the next line change the sheet number as needed
        Set excWks = excWkb.Worksheets(1)
        If bolExists Then
            intRow = excWks.UsedRange.Rows.Count + 1
        Else
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "Received"
                .Cells(1, 2) = "Data"
            End With
            intRow = 2
        End If
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 2) = FindString(olkMsg.Body, "(.*?) has exceeded its upper threshold limits for disk space by (.*?) percent")
                intRow = intRow + 1
                intCnt = intCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        If bolExists Then
            excWkb.Close True
        Else
            excWkb.SaveAs strFilename
            excWkb.Close
        End If
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    Set objFSO = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Function FindString(strText As String, strFind As String) As String
    Dim objRegEx As Object, colMatches As Object, objMatch As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = False
        .Global = True
        .Pattern = strFind
        Set colMatches = .Execute(strText)
    End With
    If colMatches.Count > 0 Then
        Set objMatch = colMatches.Item(0)
        FindString = objMatch.Value
    Else
        FindString = "Not found"
    End If
    Set objRegEx = Nothing
    Set colMatches = Nothing
    Set objMatch = Nothing
End Function

This revision is for Molly. Molly is using a custom Outlook form and needs to export the form’s custom fields along with a couple of the stock fields. This version is designed to be called from a rule, although I can modify it to run manually if that’s what Molly needs.

Adding the code to Outlook.

  1. Start Outlook
  2. Press ALT+F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Edit the code per the comments I included in it
  8. Click the diskette icon on the toolbar to save the changes
  9. Close the VB Editor
  10. Create a rule that fires for these messages
  11. Set the rule’s action to “run a script” and select HandleAnalystRequestForm as the macro to run
'Declare some constants
'On the next line edit the path to the workbook 
Const WORKBOOK_PATH = "C:\Users\David\Documents\TestArea\Molly.xlsx"
Const MACRO_NAME = "Export Messages to Excel (Rev 19)"

Sub HandleAnalystRequestForm(Item As Outlook.MailItem)
    If Item.MessageClass = "IPM.Note.Analyst Request Form" Then
        ExportFormToExcel Item, WORKBOOK_PATH, 1
    End If
End Sub

Sub ExportFormToExcel(olkMsg As Outlook.MailItem, strWorkbook As String, intSheet As Integer)
    'Declare some variables
    Dim excApp As Object, excWkb As Object, excWks As Object

    'Connect to Excel
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(strWorkbook)
    Set excWks = excWkb.Worksheets(intSheet)

    'Add a row to the spreadsheet
    intRow = excWks.UsedRange.Rows.Count + 1

    'Write the data to the spreadsheet.  Add a row for each field in the message you want to export.
    excWks.Cells(intRow, 1) = olkMsg.To
    excWks.Cells(intRow, 2) = olkMsg.CC
    excWks.Cells(intRow, 3) = olkMsg.UserProperties.Item("Trans").Value
    excWks.Cells(intRow, 4) = olkMsg.UserProperties.Item("Situation").Value
    excWks.Cells(intRow, 5) = olkMsg.UserProperties.Item("Question").Value
    excWks.Cells(intRow, 6) = olkMsg.UserProperties.Item("Potential Solution").Value
    excWks.Cells(intRow, 7) = olkMsg.UserProperties.Item("Response").Value
    excWks.Cells(intRow, 8) = olkMsg.UserProperties.Item("Next Steps").Value

    'Save and close the workbook
    excWkb.Close True

    'Clean-up open objects to avoid memory leaks
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

To be filled in later.

I put this version together for Thomas who asked me to merge various pieces of other version together for him. Outlook triggers the code via a rule. When it runs it extracts four pieces of data in the body of the message, writes the data to the spreadsheet, moves the triggering message to an Outlook folder, and runs an Excel macro. Thomas must edit the three constants at the top of the code per the instructions I included in the code for the script to work. The instructions for adding this code to Outlook are the same as for the original version of the code in this post.

'Declare some constants
'On the next line edit the path to the workbook
Const WORKBOOK_PATH = "C:\Users\David\Documents\TestArea\Kleppinger.xlsm"
'On the next line edit the path to the Outlook folder the message will be mvoed to after processing it
Const FOLDER_PATH = "Personal Folders\History"
'On the next line edit the name of the Excel macro to run after the data is added to Excel.  Be sure to include the name of the module the macro is in.
Const EXCEL_MACRO = "Sheet1.Macro_Email"
Const MACRO_NAME = "Export Messages to Excel (Rev 21)"

Sub ExportMessagesToExcel(Item As Outlook.MailItem)
    'Declare some variables
    Dim excApp As Object, excWkb As Object, excWks As Object, arrRows As Variant, arrRow As Variant, varRow As Variant, intRow
    
    'Get the folder object of the folder the message is to be moved to
    Set olkFld = OpenOutlookFolder(FOLDER_PATH)

    'Connect to Excel
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(WORKBOOK_PATH)
    Set excWks = excWkb.Worksheets(1)

    'Set the row number to change
    intRow = 1
    
    'Get data from the message and put it into the spreadsheet
    arrRows = Split(Item.Body, vbCrLf)
    For Each varRow In arrRows
        If InStr(1, varRow, ":") > 0 Then
            arrRow = Split(varRow, ":")
            Select Case arrRow(0)
                Case "A"
                    excWks.Cells(intRow, 1) = arrRow(1)
                Case "B"
                    excWks.Cells(intRow, 2) = arrRow(1)
                Case "C"
                    excWks.Cells(intRow, 3) = arrRow(1)
                Case "D"
                    excWks.Cells(intRow, 4) = arrRow(1)
            End Select
        End If
    Next
    
    'Move the message to a folder
    Item.Move olkFld
    
    'Run an Excel macro
    excApp.Run EXCEL_MACRO
    
    'Save and close the workbook
    excWkb.Close True

    'Clean-up open objects to avoid memory leaks
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision is a knock-off of Revision 5. I put it together for Yogesh Patel who is using Rev 5 and asked if I could add the ability to “… extract (save) attachments to a defined folder and also create the log.” Yogesh will need to edit the ATTACHMENT_PATH constant to point to the folder he wants to save attachments to. As it saves each attachment the code checks to see if an attachment with that name already exists in the folder. If it does, then it prepends “Copy (x)” to the file name. This precludes overwriting attachments with the same name. The instructions for adding this code to Outlook are the same as for the original version of the code in this post.

Const MACRO_NAME = "Export Messages to Excel (Rev 22)"
'On the nest line edit the path to the folder you want to save attachments to.  The path name must end with a \
Const ATTACHMENT_PATH = "c:\Users\David\Documents\TestArea\YogeshPatel\"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        olkAtt As Outlook.Attachment, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        intCount As Integer, _
        strFilename As String, _
        strAtt As String, _
        strSaveFilePath As String, _
        objFSO As Object
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "To"
            .Cells(1, 5) = "Attachments"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                excWks.Cells(intRow, 4) = olkMsg.To
                strAtt = ""
                For Each olkAtt In olkMsg.Attachments
                    If Not IsHiddenAttachment(olkAtt) Then
                        strAtt = strAtt & olkAtt.Filename & ", "
                        intCount = 0
                        strSaveFilePath = ATTACHMENT_PATH & olkAtt.Filename
                        Do While objFSO.FileExists(strSaveFilePath)
                            intCount = intCount + 1
                            strSaveFilePath = ATTACHMENT_PATH & "Copy (" & intCount & ") of " & olkAtt.Filename
                        Loop
                        olkAtt.SaveAsFile strSaveFilePath
                    End If
                Next
                If strAtt <> "" Then
                    strAtt = Left(strAtt, Len(strAtt) - 2)
                End If
                excWks.Cells(intRow, 5) = strAtt
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set objFSO = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for Tom who posted a comment asking for a version that would

  1. Be triggered by a rule.
  2. Extract text from the subject line of the message that triggered the rule. The text appears between two colons.
  3. Connect to an already open Excel workbook.
  4. Write the extracted text to column D on the first line where column D is blank.

The instructions for adding this code to Outlook are the same as for the original version of the code at the beginning of this post.

Sub ExportMessagesToExcel(olkMsg As Outlook.MailItem)
    '--> Create some constants
    'On the next line edit the name of the workbook
    Const WBK_NAME = "Book1"
    'On the next line edit the name of the worksheet
    Const WKS_NAME = "Sheet1"
    Const MACRO_NAME = "Export Messages to Excel (Rev 23)"
    
    '--> Create some variables
    Dim excApp As Object, excWkb As Object, excWks As Object, lngRow As Long, strTxt As String, intP1 As Integer, intP2 As Integer
    
    '--> Turn error handling off
    On Error Resume Next
    
    '--> Main routine
    intP1 = InStr(1, olkMsg.Subject, ":")
    intP2 = InStrRev(olkMsg.Subject, ":")
    strTxt = Mid(olkMsg.Subject, intP1 + 1, intP2 - (intP1 + 1))
    Set excApp = GetObject(, "Excel.Application")
    If TypeName(excApp) = "Nothing" Then
        MsgBox "The spreadsheet was not open.  Operation aborted.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        Set excWkb = excApp.Workbooks.Item(WBK_NAME)
        If TypeName(excWkb) = "Nothing" Then
            MsgBox "The workbook named " & WBK_NAME & " isn't open.  Operation aborted.", vbCritical + vbOKOnly, MACRO_NAME
        Else
            Set excWks = excWkb.Worksheets.Item(WKS_NAME)
            If TypeName(excWks) = "Nothing" Then
                MsgBox "The workbook doesn't have a worksheet named " & WKS_NAME & ".  Operation aborted.", vbCritical + vbOKOnly, MACRO_NAME
            Else
                lngRow = 1
                Do Until excWks.Cells(lngRow, 4) = ""
                    lngRow = lngRow + 1
                Loop
                excWks.Cells(lngRow, 4) = strTxt
            End If
        End If
    End If
    
    '--> Cleanup
    Set excApp = Nothing
    Set excWkb = Nothing
    Set excWks = Nothing
End Sub

This revision is for Tanveer who asked for a knockoff of revision 6 with the added ability of giving each saved attachment a unique filename. The filename will be the attachment name plus an incremental number.

The instructions for adding this code to Outlook are the same as for the original version of the code in this post.

Const MACRO_NAME = "Export Messages to Excel (Rev 24)"

Sub ExportMessagesToExcel()
    'Edit the path on the next line.  This is the folder the code will save the attachments to.  Make sure that the path ends with a \
    Const EXPORT_FOLDER = "C:\SomeFolder\"
    Dim olkLst As Object, _
        olkMsg As Object, _
        olkAtt As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        objFSO As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        intCount As Integer, _
        strFilename As String, _
        strDateRange As String, _
        strTemp As String, _
        arrTemp As Variant, _
        datStart As Date, _
        datEnd As Date
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
        arrTemp = Split(strDateRange, "to")
        datStart = IIf(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
        datEnd = IIf(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
        intVersion = GetOutlookVersion()
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "Attachments"
        End With
        intRow = 2
        'Write messages to spreadsheet
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                For Each olkAtt In olkMsg.Attachments
                    If Not IsHiddenAttachment(olkAtt) Then
                        intCount = 0
                        strTemp = olkAtt.Filename
                        Do While objFSO.FileExists(EXPORT_FOLDER & strTemp)
                            intCount = intCount + 1
                            strTemp = objFSO.GetBaseName(olkAtt.Filename) & " " & intCount & "." & objFSO.GetExtensionName(olkAtt.Filename)
                        Loop
                        olkAtt.SaveAsFile EXPORT_FOLDER & strTemp
                        excWks.Cells(intRow, 4).Value = excWks.Cells(intRow, 4).Value & strTemp & vbCrLf
                    End If
                Next
                If Len(excWks.Cells(intRow, 4).Value) > 0 Then
                    excWks.Cells(intRow, 4).Value = Left(excWks.Cells(intRow, 4).Value, Len(excWks.Cells(intRow, 4).Value) - 1)
                End If
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set olkLst = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for Nicholas K. who asked for a solution that parses out all the hyperlinks in each message and writes them to successive columns in the spreadsheet. With the exception of that addition, this version is identical to the code from the original post. Nicholas will use the instructions from the original post to add this code to Outlook.

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCol As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        arrHyp As Variant, _
        varhyp As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", "Export Messages to Excel")
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                intCol = 4
                arrHyp = Split(GetLinks(olkMsg.HTMLBody), "|")
                For Each varhyp In arrHyp
                    excWks.Cells(intRow, intCol) = varhyp
                    intCol = intCol + 1
                Next
                intRow = intRow + 1
            End If
        Next
        excWks.Columns("A:ZZ").AutoFit
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function GetLinks(strHTML As String) As String
    Const READYSTATE_COMPLETE = 4
    Dim objIE As Object, objDoc As Object, colLinks As Object, objLink As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.navigate "about:blank"
    Do Until objIE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    objIE.document.Body.innerHTML = strHTML
    Set objDoc = objIE.document
    Set colLinks = objDoc.getElementsByTagName("a")
    If colLinks.Length > 0 Then
        For Each objLink In colLinks
            GetLinks = GetLinks & objLink.href & "|"
        Next
        GetLinks = Left(GetLinks, Len(GetLinks) - 1)
    Else
        GetLinks = ""
    End If
    Set objLink = Nothing
    Set colLinks = Nothing
    Set objDoc = Nothing
    Set objIE = Nothing
End Function

This revision is for Malcolm who needs to extract two pieces of information from the body of certain messages. The code is a knock-off of revision 14, changed to watch for the key text in Macolm’s messages and extract the data that appears after it. Malcolm will use the instructions from the original post to add the code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 26)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intVersion As Integer, _
        strFilename As String, _
        strTemp As String, _
        arrLines As Variant, _
        varLine As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "Contact #"
            .Cells(1, 5) = "Email"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                arrLines = Split(olkMsg.Body, vbCrLf)
                For Each varLine In arrLines
                    strTemp = Trim(varLine)
                    If Left(strTemp, 16) = "Contact number: " Then
                        excWks.Cells(intRow, 4) = Mid(strTemp, 17)
                    Else
                        If Left(strTemp, 15) = "Email Address: " Then
                            excWks.Cells(intRow, 5) = Mid(strTemp, 16)
                        End If
                    End If
                Next
                intRow = intRow + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Laura. Laura receives messages from an automated process each containing an HTML table in the message body. She needs to pull three pieces of data from that table, each piece located in a different cell. To do that, I wrote a function that returns an array of the cells, then simply get the contents of the three cells Laura needs. The rest of the code is essentially the same as that in the original post. Laura will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 27)"

Private Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCnt As Integer, _
        strFilename As String, _
        arrCells As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Date"
            .Cells(1, 4) = "Email"
            .Cells(1, 5) = "Count"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Get the cells from the table in the body of the message
                arrCells = Split(GetCells(olkMsg.HTMLBody), Chr(255))
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = olkMsg.Subject
                excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 3) = Left(arrCells(12), 19)
                excWks.Cells(intRow, 4) = arrCells(16)
                excWks.Cells(intRow, 5) = arrCells(14)
                intRow = intRow + 1
                intCnt = intCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetCells(strHTML As String) As String
    Const READYSTATE_COMPLETE = 4
    Dim objIE As Object, objDoc As Object, colCells As Object, objCell As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.navigate "about:blank"
    Do Until objIE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    objIE.document.Body.innerHTML = strHTML
    Set objDoc = objIE.document
    Set colCells = objDoc.getElementsByTagName("td")
    If colCells.Length > 0 Then
        For Each objCell In colCells
            GetCells = GetCells & objCell.innerText & Chr(255)
        Next
        GetCells = Left(GetCells, Len(GetCells) - 1)
    Else
        GetCells = ""
    End If
    Set objCell = Nothing
    Set colCells = Nothing
    Set objDoc = Nothing
    Set objIE = Nothing
End Function

This revision is for Aiyana. Aiyana was sent a message with multiple message items attached. For each attachment, Aiyana needs to parse data from the body of the item and put it in certain rows in the spreadsheet. The data from each message will go in one column of the spreadsheet. Aiyana will use the instructions from the original post to add this code to Outlook. To use the script, Aiyana will select the message containing the attachments and then run the macro.

Const MACRO_NAME = "Export Messages to Excel (Rev 28)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        olkAtt As Object, _
        olkTmp As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intCol As Integer, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        intCol = 1
        'Write attachment bodies to the spreadsheet
        Set olkMsg = Application.ActiveExplorer.Selection(1)
        For Each olkAtt In olkMsg.Attachments
            If Right(LCase(olkAtt.Filename), 4) = ".msg" Then
                olkAtt.SaveAsFile Environ("TEMP") & "\" & olkAtt.Filename
                Set olkTmp = Application.CreateItemFromTemplate(Environ("TEMP") & "\" & olkAtt.Filename)
                excWks.Cells(1, intCol) = StripChar(Replace(FindString(olkTmp.Body, "name:(.+)\r\n"), "name: ", ""))
                excWks.Cells(2, intCol) = StripChar(Replace(FindString(olkTmp.Body, "email:(.+)\r\n"), "email: ", ""))
                excWks.Cells(3, intCol) = StripChar(Replace(FindString(olkTmp.Body, "company:(.+)\r\n"), "company: ", ""))
                excWks.Cells(4, intCol) = StripChar(Replace(FindString(olkTmp.Body, "city:(.+)\r\n"), "city: ", ""))
                excWks.Cells(5, intCol) = StripChar(Replace(FindString(olkTmp.Body, "branch:(.+)\r\n"), "branch: ", ""))
                excWks.Cells(6, intCol) = StripChar(Replace(FindString(olkTmp.Body, "design:(.+)\r\n"), "design: ", ""))
                excWks.Cells(7, intCol) = StripChar(Replace(FindString(olkTmp.Body, "satisfaction:(.+)\r\n"), "satisfaction: ", ""))
                excWks.Cells(8, intCol) = StripChar(Replace(FindString(olkTmp.Body, "response:(.+)\r\n"), "response: ", ""))
                excWks.Cells(9, intCol) = StripChar(Replace(FindString(olkTmp.Body, "frequency:(.+)\r\n"), "frequency: ", ""))
                excWks.Cells(10, intCol) = StripChar(Replace(FindString(olkTmp.Body, "delivery:(.+)\r\n"), "delivery: ", ""))
                excWks.Cells(11, intCol) = StripChar(Replace(FindString(olkTmp.Body, "aftersales:(.+)\r\n"), "aftersales: ", ""))
                excWks.Cells(12, intCol) = StripChar(Replace(FindString(olkTmp.Body, "total:(.+)\r\n"), "total: ", ""))
                excWks.Cells(13, intCol) = StripChar(Replace(FindString(olkTmp.Body, "referencecompany:(.+)\r\n"), "referencecompany: ", ""))
                excWks.Cells(14, intCol) = StripChar(Replace(FindString(olkTmp.Body, "referencecontact:(.+)\r\n"), "referencecontact: ", ""))
                excWks.Cells(15, intCol) = StripChar(Replace(FindString(olkTmp.Body, "referenceemail:(.+)\r\n"), "referenceemail: ", ""))
                excWks.Cells(16, intCol) = StripChar(Replace(FindString(olkTmp.Body, "message:(.+)\b"), "message: ", ""))
                intCol = intCol + 1
            End If
            Set olkTmp = Nothing
        Next
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set olkMsg = Nothing
    Set olkAtt = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function FindString(strText As String, strFind As String) As String
    Dim objRegEx As Object, colMatches As Object, objMatch As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Global = True
        .Pattern = strFind
        Set colMatches = .Execute(strText)
    End With
    If colMatches.Count > 0 Then
        Set objMatch = colMatches.Item(0)
        FindString = objMatch.Value
    Else
        FindString = "Not found"
    End If
    Set objRegEx = Nothing
    Set colMatches = Nothing
    Set objMatch = Nothing
End Function

Private Function StripChar(strValue As String) As String
    StripChar = Replace(strValue, vbCr, "")
    StripChar = Replace(StripChar, vbLf, "")
    StripChar = Replace(StripChar, vbTab, "")
    StripChar = Trim(StripChar)
End Function 

I created this revision for Juan Carlos Rodriguez. Juan asked for a version that exports all the messages in the selected folder and all that folder’s sub-folders with a couple of twists. He wants the export to write each message’s subject, recipients (both those it was sent to and those that were CC’d), the sender’s name, a list of attachments, and the message’s unique ID. Juan also wants the export to create a file system folder for each message and save a copy of the message, in plain-text format, and all attachments to that folder. Finally, he wants a link to each message’s folder added to the spreadsheet.

Juan will follow the instructions from the original post to add the code to Outlook. When he is ready to run the macro, he will

  1. Select a folder in Outlook
  2. Run the macro RunExportMessagesToExcel
'--> Declare some constants
'On the next line edit the path to the folder you want to export the messages to.  The macro will create a sub-folder under this folder for each message.
Const ROOT_PATH = "C:\Users\David\Documents\TestArea\JCR"
Const MACRO_NAME = "Export Messages to Excel (Rev 29)"

'--> Declare some variables
Private excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        objFSO As Object, _
        lngCnt As Long, _
        lngRow As Long

Sub RunExportMessagesToExcel()
    '--> Declare some variables
    Dim strWkb As String
    strWkb = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strWkb <> "" Then
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Connect to Excel
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Folder"
            .Cells(1, 2) = "Subject"
            .Cells(1, 3) = "To"
            .Cells(1, 4) = "From"
            .Cells(1, 5) = "CC"
            .Cells(1, 6) = "Attachments"
            .Cells(1, 7) = "ID"
            .Cells(1, 8) = "Link"
        End With
        lngRow = 2
        ExportMessagesToExcel Application.ActiveExplorer.CurrentFolder
        excWks.Columns("A:H").AutoFit
        excWkb.SaveAs strWkb
        excWkb.Close
        Set excWks = Nothing
        Set excWkb = Nothing
        Set excApp = Nothing
        Set objFSO = Nothing
        MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
    Else
        MsgBox "Operation canceled.", vbInformation + vbOKOnly, MACRO_NAME
    End If
End Sub

Private Sub ExportMessagesToExcel(olkFol As Outlook.MAPIFolder)
    '--> Declare some variables
    Dim olkMsg As Object, _
        olkAtt As Object, _
        olkSub As Object, _
        objFol As Object, _
        strAtt As String
    'Process the current folder and write its messages to the spreadsheet
    For Each olkMsg In olkFol.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            strAtt = ""
            'Create a new file system folder to store the message and its attachments
            Set objFol = objFSO.CreateFolder(ROOT_PATH & "\" & olkMsg.EntryID)
            'Save a plain-text version of the email to the folder
            olkMsg.SaveAs objFol.Path & "\Message.txt", olTXT
            'Save the attachments to the folder
            For Each olkAtt In olkMsg.Attachments
                If Not IsHiddenAttachment(olkAtt) Then
                    olkAtt.SaveAsFile objFol.Path & "\" & olkAtt.Filename
                    strAtt = strAtt & olkAtt.Filename & ", "
                End If
            Next
            If Len(strAtt) > 0 Then strAtt = Left(strAtt, Len(strAtt) - 2)
            'Add a row for each field in the message you want to export
            excWks.Cells(lngRow, 1) = olkMsg.Parent.FolderPath
            excWks.Cells(lngRow, 2) = olkMsg.Subject
            excWks.Cells(lngRow, 3) = olkMsg.To
            excWks.Cells(lngRow, 4) = olkMsg.SenderName
            excWks.Cells(lngRow, 5) = olkMsg.CC
            excWks.Cells(lngRow, 6) = strAtt
            excWks.Cells(lngRow, 7) = olkMsg.EntryID
            excWks.Hyperlinks.Add Anchor:=excWks.Range("H" & lngRow, "H" & lngRow), Address:="file://" & objFol.Path, TextToDisplay:="View Original Items"
            lngRow = lngRow + 1
            lngCnt = lngCnt + 1
            Set olkAtt = Nothing
        End If
        DoEvents
    Next
    Set olkMsg = Nothing
    Set objFol = Nothing
    For Each olkSub In olkFol.Folders
        ExportMessagesToExcel olkSub
        DoEvents
    Next
    Set olkSub = Nothing
End Sub

Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    ' Purpose: Determines if an attachment is a hidden attachment.
    ' Written: 7/12/2012
    ' Author:  David Lee
    ' Outlook: 2007 and later
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I created this revision is for Suhas. Suhas asked for a mashup of capabilities from multiple revisions. Specifically, Suhas wants a version that

  1. Ask for the path and folder name to save the data
  2. Ask for a date range
  3. Export the data from the subfolders based on the date range into the same excel sheet

This version does all three. Suhas will use the instructions from the original post to add the code to Outlook. To run the code, Suhas will

  1. Select a folder. The macro will process that folder and all sub-folders under it.
  2. Run the macro ExportMessagesToExcel.
  3. Enter the path and filename of the file the macro will export the data to. The macro will create this file.
  4. Enter a date range. The macro will only export messages that fall inside the date range Suhas enters.
Const MACRO_NAME = "Export Messages to Excel (Rev 30)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    lngMessages As Long, _
    lngRow As Long, _
    datStart As Date, _
    datEnd As Date

Sub ExportMessagesToExcel()
    Dim strFilename As String, strDateRange As String, arrTemp As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
        arrTemp = Split(strDateRange, "to")
        datStart = IIf(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
        datEnd = IIf(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
        lngMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        lngRow = excWks.UsedRange.Rows.Count + 1
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        ProcessFolder Application.ActiveExplorer.CurrentFolder
        excWkb.SaveAs strFilename
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngMessages & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkLst As Object, _
        olkMsg As Object, _
        olkSub As Object
    If olkFld.DefaultItemType = olMailItem Then
        Set olkLst = olkFld.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        'Write messages to spreadsheet
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(lngRow, 1) = olkMsg.Subject
                excWks.Cells(lngRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(lngRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                lngRow = lngRow + 1
                lngMessages = lngMessages + 1
            End If
        Next
        Set olkMsg = Nothing
    End If
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkSub = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This is another revision I put together for Saroj. Every hour Saroj receives a message with a body like this

Provider: AFU Service: toxxxxfile MaxLimit: 1 Active: 0 Total: 0
Tasks:
Provider: AFURVGX Service: fltocvx MaxLimit: 5 Active: 0 Total: 1
Tasks:
P1406624a0ef5136ac692
Provider: AFU Service: extractor MaxLimit: 87 Active: 7 Total: 7
Tasks:
Provider: AFU Service: pllxxltoxt MaxLimit: 9 Active: 0 Total: 0
Tasks:
Provider: AFU Service: catly5tooneas MaxLimit: 3 Active: 0 Total: 0
Tasks:
Provider: AFUR Service: urdestoxt MaxLimit: 6 Active: 0 Total: 0
Tasks:
Provider: AFU Service: slcreate MaxLimit: 6 Active: 0 Total: 0
Tasks:
Provider: AFUR Service: caty5tocvx MaxLimit: 23 Active: 5 Total: 8

He needs to be able to extract the number after the word “Active:” and write it to a spreadsheet. All the values from one message will go in different columns on the same row. The solution I’ve put together extracts the same message information as the original post plus the values Saroj needs.

Saroj will follow the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 31)"

Private Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Long, _
        lngCol As Long, _
        lngCnt As Long, _
        intVersion As Integer, _
        strFilename As String, _
        arrHits As Variant, _
        varHit As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        lngRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(lngRow, 1) = olkMsg.Subject
                excWks.Cells(lngRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(lngRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                lngCol = 4
                arrHits = Split(FindString(olkMsg.Body, "Active:\s[0-9]*\s"), "|")
                For Each varHit In arrHits
                    excWks.Cells(lngRow, lngCol) = Trim(Replace(varHit, "Active: ", ""))
                    lngCol = lngCol + 1
                Next
                lngRow = lngRow + 1
                lngCnt = lngCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function FindString(strText As String, strFind As String) As String
    Dim objRegEx As Object, colMatches As Object, objMatch As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = False
        .Global = True
        .Pattern = strFind
        Set colMatches = .Execute(strText)
    End With
    If colMatches.Count > 0 Then
        For Each objMatch In colMatches
            FindString = FindString & objMatch.Value & "|"
        Next
        FindString = Left(FindString, Len(FindString) - 1)
    Else
        FindString = "Not found"
    End If
    Set objRegEx = Nothing
    Set colMatches = Nothing
    Set objMatch = Nothing
End Function

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for Jonathan Xiang who asked for a knock-off of rev 30 with a few changes. Specifically

1. I would like to hard-code the sub-folders that the macro has to extract the messages from; And only those folders have to be processed.
2. Instead of a date range; I would like to extract the data of a specific date.
3. If the subject line of the message is empty, then it should skip exporting that message to excel.
4. When extracting to an excel file it should automatically load a pre-defined template and extract the data into “sheet1″.

This rev incorporates all the changes Jonathan asked for. Jonathan can use the instructions from the original post to add the code to Outlook. He will also need to edit two lines at the top of the code per the comments I included in it.

Const MACRO_NAME = "Export Messages to Excel (Rev 32)"
'On the next line edit the list of Outlook folders you want to process. Each folder name must be the path to a folder. Folder names are separated by a comma.
Const FOLDER_LIST = "mailbox\Projects\Project1,personal folders\marketing"
'On the next line edit the path to and name of the Excel template file.
Const TEMPLATE_NAME = "C:\Cooldude_Template.xlsx"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVersion As Integer, _
    lngMessages As Long, _
    lngRow As Long, _
    datStart As Date, _
    datEnd As Date

Private Sub ExportMessagesToExcel()
    Dim strFilename As String, strDateRange As String, arrFolders As Variant, varFolder As Variant
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        strDateRange = InputBox("Enter the date you want to export messages for in the form ""mm/dd/yy""", MACRO_NAME, Date)
        If IsDate(strDateRange) Then
            datStart = strDateRange & " 12:00am"
            datEnd = strDateRange & " 11:59pm"
        Else
            datStart = Date & " 12:00am"
            datEnd = Date & " 11:59pm"
        End If
        lngMessages = 0
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add(TEMPLATE_NAME)
        Set excWks = excWkb.Worksheets(1)
        lngRow = excWks.UsedRange.Rows.Count + 1
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        arrFolders = Split(FOLDER_LIST, ",")
        For Each varFolder In arrFolders
            ProcessFolder OpenOutlookFolder(CStr(varFolder))
        Next
        excWkb.SaveAs strFilename
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngMessages & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkLst As Object, _
        olkMsg As Object
    If olkFld.DefaultItemType = olMailItem Then
        Set olkLst = olkFld.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        'Write messages to spreadsheet
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                If olkMsg.Subject <> "" Then
                    'Add a row for each field in the message you want to export
                    excWks.Cells(lngRow, 1) = olkMsg.Subject
                    excWks.Cells(lngRow, 2) = olkMsg.ReceivedTime
                    excWks.Cells(lngRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                    lngRow = lngRow + 1
                    lngMessages = lngMessages + 1
                End If
            End If
        Next
        Set olkMsg = Nothing
    End If
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

I put together this revision for Jesper Jespersen who asked for a mashup of features from revisions 1, 3, and 7. From rev 1 Jesper wants the the ability to specifiy which Outlook folders to process. From rev 3 he wants the ability to process sub-folders. From rev 7 he wants the ability to append the exported data to an existing workbook rather than creating a new one. Jesper also asked for the ability to filter messages based on keywords in the subject line and to export either the time the message was sent or received depending on whether he sent or received the message. This revision contains everything Jesper asked for. Jesper can use the instructions from the original post to add the code to Outlook.

'On the next line edit the list of keywords to search for.  Each keyword will be separated from the previous keyword by a |.
Const KEYWORDS = "word1|word2"
Const MACRO_NAME = "Export Messages to Excel (Rev 33)"

Dim lngRow As Long, _
    lngMessages As Long, _
    intVersion As Integer, _
    excApp As Object, _
    excWkb As Object, _
    excWks As Object
    
Sub ExportController()
    intVersion = GetOutlookVersion()
    'Add as many exports as you need. Each export is controlled by a line like the following
    'The format of the command is ExportMessagesToExcel "Path to Workbook", "Sheet Number", "Path to Outlook Folder"
    ExportMessagesToExcel "C:\Folder1.xlsx", 1, "Personal Folders\Folder1"
    ExportMessagesToExcel "C:\Folder2.xlsx", 1, "Personal Folders\Folder2"
    MsgBox "Process complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Sub ExportMessagesToExcel(strFilename As String, intSheet As Integer, strFolderPath As String)
    Dim olkFld As Object
    If strFilename = "" Then
        MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        If strFolderPath = "" Then
            MsgBox "The folderpath was empty.", vbCritical + vbOKOnly, MACRO_NAME
        Else
            Set olkFld = OpenOutlookFolder(strFolderPath)
            If TypeName(olkFld) = "Nothing" Then
                MsgBox "The folder pointed to by the path '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
            Else
                Set excApp = CreateObject("Excel.Application")
                Set excWkb = excApp.Workbooks.Open(strFilename)
                Set excWks = excWkb.Worksheets(intSheet)
                lngRow = excWks.UsedRange.rows.Count + 1
                ProcessFolder olkFld
                excWkb.Close SaveChanges:=True
            End If
        End If
    End If
    Set olkFld = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            If ContainsKeyword(olkMsg.Subject) Then
                excWks.Cells(lngRow, 1) = olkMsg.Subject
                excWks.Cells(lngRow, 2) = IIf(olkMsg.SenderEmailAddress = Session.CurrentUser.Address, olkMsg.SentOn, olkMsg.ReceivedTime)
                excWks.Cells(lngRow, 3) = olkMsg.To
                excWks.Cells(lngRow, 4) = olkMsg.SenderEmailAddress
                excWks.Cells(lngRow, 5) = olkMsg.Parent.FolderPath
                lngRow = lngRow + 1
                lngMessages = lngMessages + 1
            End If
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkssub = Nothing
End Sub

Private Function ContainsKeyword(strText As String) As Boolean
    Dim objRegEx As Object, colMatches As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Pattern = KEYWORDS
        .Global = True
    End With
    Set colMatches = objRegEx.Execute(strText)
    ContainsKeyword = (colMatches.Count > 0)
    Set objRegEx = Nothing
    Set colMatches = Nothing
End Function

Public Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Shawn who asked for a version that will

  • Create a new spreadsheet or update an existing spreadsheet.
  • Export messages from a folder based on a date range.
  • Save all attachments from the processed messages to a file system folder.
  • Hyperlink to each saved attachment from the spreadsheet.

To avoid the possibility of duplicate file names, Shawn asked to have a date stamp added to each attachment’s file name.

Shawn can use the instructions from the original post to add the code to Outlook.

'--> Declare some constants
'Edit the path on the next line.  This is the folder the code will save the attachments to.  Make sure the path ends with a \
Const EXPORT_FOLDER = "C:\Users\David\Documents\TestArea\Shawn_Tester\"
Const MACRO_NAME = "Export Messages to Excel (Rev 34)"

Private Sub ExportMessagesToExcel()
    '--> Declare some variables
    Dim olkLst As Object, _
        olkMsg As Object, _
        olkAtt As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        objFSO As Object, _
        lngRow As Long, _
        lngCol As Long, _
        lngCnt As Long, _
        intVersion As Integer, _
        strFilename As String, _
        strAttFilename As String, _
        strDateRange As String, _
        arrTemp As Variant, _
        datStart As Date, _
        datEnd As Date, _
        bolNew As Boolean
        
    '--> Initialize some variables
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    intVersion = GetOutlookVersion()
    Set excApp = CreateObject("Excel.Application")
    
    '--> Get a date range from the user.  Parse what they enter into starting and ending dates.
    strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
    arrTemp = Split(strDateRange, "to")
    datStart = IIf(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
    datEnd = IIf(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
    
    '--> Main routine
    If IsDate(datStart) And IsDate(datEnd) Then
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & " " & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        If olkLst.Count > 0 Then
            strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
            If strFilename = "" Then
                MsgBox "You did not enter a filename.  Operation cancelled.", vbInformation + vbOKOnly, MACRO_NAME
            Else
                If objFSO.FileExists(strFilename) Then
                    Set excWkb = excApp.Workbooks.Open(strFilename)
                    Set excWks = excWkb.Worksheets(1)
                    lngRow = excWks.UsedRange.rows.Count + 1
                Else
                    Set excWkb = excApp.Workbooks.Add()
                    Set excWks = excWkb.Worksheets(1)
                    lngRow = 2
                    With excWks
                        .Cells(1, 1) = "Subject"
                        .Cells(1, 2) = "Received"
                        .Cells(1, 3) = "Sender"
                        .Cells(1, 4) = "Attachments"
                    End With
                    bolNew = True
                End If
                For Each olkMsg In olkLst
                    If olkMsg.Class = olMail Then
                        lngCol = 4
                        'Add a row for each field in the message you want to export
                        excWks.Cells(lngRow, 1) = olkMsg.Subject
                        excWks.Cells(lngRow, 2) = olkMsg.ReceivedTime
                        excWks.Cells(lngRow, 3) = GetSMTPAddress(olkMsg, intVersion)
                        For Each olkAtt In olkMsg.Attachments
                            If Not IsHiddenAttachment(olkAtt) Then
                                strAttFilename = EXPORT_FOLDER & objFSO.GetBaseName(olkAtt.Filename) & Format(olkMsg.ReceivedTime, "yyyy-mm-dd-hh-nn-ss") & "." & objFSO.GetExtensionName(olkAtt.Filename)
                                olkAtt.SaveAsFile strAttFilename
                                excWks.Hyperlinks.Add Anchor:=excWks.Range(NumberToLetter(lngCol) & lngRow), Address:="FILE:\\" & strAttFilename, TextToDisplay:=olkAtt.Filename
                                lngCol = lngCol + 1
                            End If
                        Next
                        lngRow = lngRow + 1
                        lngCnt = lngCnt + 1
                    End If
                Next
                If bolNew Then
                    excWkb.SaveAs strFilename
                    excWkb.Close
                Else
                    excWkb.Close True
                End If
            End If
        Else
            MsgBox "I did not find any items that arrived between " & datStart & " and " & datEnd & ".  Operation cancelled.", vbInformation + vbOKOnly, MACRO_NAME
        End If
    End If

    '--> Destroy all objects
    Set olkLst = Nothing
    Set olkMsg = Nothing
    Set olkAtt = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    Set objFSO = Nothing
    
    '--> Let the user know how many items were exported
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function NumberToLetter(ByVal intNumber As Integer) As String
    NumberToLetter = Chr(64 + intNumber)
End Function

I created this revision for Stefan. Stefan receives messages containing a single HTML table. He asked if I could build a solution that parses the table and exports the data to Excel. The table contains a variable number of rows with each row containing a fixed number of columns. Stefan wants the exported data to be laid out exactly as it was in the table. One of the columns contains a very long number which Excel normally displays using scientific notation (e.g. 1.00E+12). Stefan asked if I could also force that column to display as a regular number.

Stefan can use the instructions from the original post to add the code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 35)"

Private Sub ExportMessagesToExcel()
    Const CELLS_PER_ROW = 14
    Dim olkFld As Outlook.MAPIFolder, _
        olkMsg As Outlook.MailItem, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        arrCel As Variant, _
        varCel As Variant, _
        lngRow As Long, _
        lngCol As Long, _
        intPtr As Integer
    Set olkFld = Session.PickFolder
    If TypeName(olkFld) = "Nothing" Then
        MsgBox "You did not select a folder.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        excApp.Visible = True
        With excWks
            .Cells(1, 1) = "Time"
            .Cells(1, 2) = "ReceipientID"
            .Cells(1, 3) = "Shard"
            .Cells(1, 4) = "SenderEmail"
            .Cells(1, 5) = "Status"
            .Cells(1, 6) = "SenderClientIP"
            .Cells(1, 7) = "SenderID"
            .Cells(1, 8) = "SederName"
            .Cells(1, 9) = "Group"
            .Cells(1, 10) = "MailCount"
            .Cells(1, 11) = "SampleSubject"
            .Cells(1, 12) = "SampleNumber"
            .Cells(1, 13) = "SumNumber"
            .Cells(1, 14) = "SampleBody"
        End With
        lngRow = 2
        For Each olkMsg In olkFld.Items
            arrCel = Split(GetCells(olkMsg.HTMLBody), Chr(255))
            lngCol = 1
            For intPtr = LBound(arrCel) To UBound(arrCel)
                If lngCol = 2 Then
                    excWks.Cells(lngRow, lngCol).NumberFormat = "@"
                End If
                excWks.Cells(lngRow, lngCol).Value = arrCel(intPtr)
                If (intPtr + 1) Mod CELLS_PER_ROW = 0 Then
                    lngRow = lngRow + 1
                    lngCol = 1
                Else
                    lngCol = lngCol + 1
                End If
            Next
        Next
        excWks.Columns("A:N").AutoFit
        excApp.Visible = True
        Set excWks = Nothing
        Set excWkb = Nothing
        Set excApp = Nothing
    End If
    Set olkFld = Nothing
End Sub

Private Function GetCells(strHTML As String) As String
    Const READYSTATE_COMPLETE = 4
    Dim objIE As Object, objDoc As Object, colCells As Object, objCell As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.navigate "about:blank"
    Do Until objIE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    objIE.document.Body.innerHTML = strHTML
    Set objDoc = objIE.document
    Set colCells = objDoc.getElementsByTagName("td")
    If colCells.Length > 0 Then
        For Each objCell In colCells
            GetCells = GetCells & objCell.innerText & Chr(255)
        Next
        GetCells = Left(GetCells, Len(GetCells) - 1)
    Else
        GetCells = ""
    End If
    Set objCell = Nothing
    Set colCells = Nothing
    Set objDoc = Nothing
    objIE.Quit
    Set objIE = Nothing
End Function 

I created this revision for Luis. It is identical to the code in the original post with the exception that Luis also needs the To and Body fields of the message.

Luis can use the instructions from the original post to add the code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 36)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCnt As Integer, _
        intVersion As Integer, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename <> "" Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Sender"
            .Cells(1, 2) = "Receiver"
            .Cells(1, 3) = "Received"
            .Cells(1, 4) = "Subject"
            .Cells(1, 5) = "Body"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = GetSMTPAddress(olkMsg, intVersion)
                excWks.Cells(intRow, 2) = olkMsg.To
                excWks.Cells(intRow, 3) = olkMsg.ReceivedTime
                excWks.Cells(intRow, 4) = olkMsg.Subject
                excWks.Cells(intRow, 5) = olkMsg.Body
                intRow = intRow + 1
                intCnt = intCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFilename
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.VERSION, ".")
    GetOutlookVersion = arrVer(0)
End Function

Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for a reader named Cleverson who asked for a couple of changes to the original version. Specifically, Cleverson asked for a version that exports a given folder each time rather than having to select a folder to export. He also asked me to rearrange the order of the exported fields.

Cleverson can use the instructions from the original post to add the code to Outlook.

'On the next line edit the path to the Outlook folder you want to export
Const FOLDER_PATH = "Mailbox - Doe, John\Inbox"
Const MACRO_NAME = "Export Messages to Excel (Rev 37)"

Private Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        olkFld As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCnt As Integer, _
        intVersion As Integer, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename = "" Then
        MsgBox "You did not enter the path and name of a file to save the export to.  Export cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        Set olkFld = OpenOutlookFolder(FOLDER_PATH)
        If TypeName(olkFld) = "Nothing" Then
            MsgBox "The folder " & FOLDER_PATH & " does not exist.  Export cancelled.", vbCritical + vbOKOnly, MACRO_NAME
        Else
            intVersion = GetOutlookVersion()
            Set excApp = CreateObject("Excel.Application")
            Set excWkb = excApp.Workbooks.Add()
            Set excWks = excWkb.Worksheets(1)
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "Received"
                .Cells(1, 2) = "Sender"
                .Cells(1, 3) = "Subject"
            End With
            intRow = 2
            'Write messages to spreadsheet
            For Each olkMsg In olkFld.Items
                'Only export messages, not receipts or appointment requests, etc.
                If olkMsg.Class = olMail Then
                    'Add a row for each field in the message you want to export
                    excWks.Cells(intRow, 1) = olkMsg.ReceivedTime
                    excWks.Cells(intRow, 2) = GetSMTPAddress(olkMsg, intVersion)
                    excWks.Cells(intRow, 3) = olkMsg.Subject
                    intRow = intRow + 1
                    intCnt = intCnt + 1
                End If
            Next
            Set olkMsg = Nothing
            excWks.Columns("A:C").AutoFit
            excWkb.SaveAs strFilename
            excWkb.Close
            MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
        End If
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function

This revision goes out to a reader named Allen who needs to export data from a custom Outlook form rather than a stock Outlook message. Allen only wants to export the user-defined fields he’s added to his custom form. While this revision will only work for Allen’s form, it demonstrates how an export can handle custom form fields. All you need to know is the name of the custom form (required to ensure that we only process instances of that form and not regular messages which wouldn’t have the custom fields) and the names of the fields on the custom form.

Allen will use the instructions from the original post to add this code to Outlook.

Sub ExportMessagesToExcel()
    'On the next line edit the custom form's name as needed.
    Const CUSTOM_FORM_NAME = "IPM.Note.Near Miss Log"
    Const MACRO_NAME = "Export Messages to Excel (Rev 38)"
    Dim olkMsg As Object, _
        olkUDP As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Long, _
        lngCnt As Long, _
        strFilename As String
    strFilename = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFilename = "" Then
        MsgBox "You failed to enter a filename.  Export cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Incident"
            .Cells(1, 2) = "Corrective Action"
            .Cells(1, 3) = "GRG"
            .Cells(1, 4) = "Incident Date"
            .Cells(1, 5) = "Location Drop"
            .Cells(1, 6) = "Location Name"
            .Cells(1, 7) = "Medical Treatment"
            .Cells(1, 8) = "Message"
            .Cells(1, 9) = "Name1"
            .Cells(1, 10) = "Potential Root Cause"
            .Cells(1, 11) = "Time1"
            .Cells(1, 12) = "Work Area"
        End With
        lngRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages that use a specific custom form
            If olkMsg.MessageClass = CUSTOM_FORM_NAME Then
                'Add a row for each field in the message you want to export
                For Each olkUDP In olkMsg.UserProperties
                    Select Case olkUDP.Name
                        Case "Brief Description of Incident:"
                            excWks.Cells(lngRow, 1) = olkUDP.Value
                        Case "Description of Corrective Action"
                            excWks.Cells(lngRow, 2) = olkUDP.Value
                        Case "GRG"
                            excWks.Cells(lngRow, 3) = olkUDP.Value
                        Case "Incident Date:"
                            excWks.Cells(lngRow, 4) = olkUDP.Value
                        Case "Location Drop"
                            excWks.Cells(lngRow, 5) = olkUDP.Value
                        Case "Location Name:"
                            excWks.Cells(lngRow, 6) = olkUDP.Value
                        Case "Medical Treatment"
                            excWks.Cells(lngRow, 7) = olkUDP.Value
                        Case "Message"
                            excWks.Cells(lngRow, 8) = olkMsg.Body
                        Case "name1"
                            excWks.Cells(lngRow, 9) = olkUDP.Value
                        Case "Potential Root Cause:"
                            excWks.Cells(lngRow, 10) = olkUDP.Value
                        Case "Time1"
                            excWks.Cells(lngRow, 11) = olkUDP.Value
                        Case "Work Area:"
                            excWks.Cells(lngRow, 12) = olkUDP.Value
                    End Select
                Next
                lngRow = lngRow + 1
                lngCnt = lngCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        Set olkUDP = Nothing
        excWks.Columns("A:L").AutoFit
        excWkb.SaveAs strFilename
        excWkb.Close
        MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
End Sub

This revision is for Bobby, a reader who asked for a version to handle exporting messages with a unique format. Bobby receives messages about new hires in his company. For each of these messages, Bobby needs to export the new employee’s name, the date they begin work, and the location they’ll be working at. Bobby wants to write these messages to an existing spreadsheet. He will run the macro manually and it will always look for the messages in a specific folder.

Bobby will use the instructions from the original post to add this code to Outlook.

'--> On the next line edit the path to and name of the workbook the extracted data is to be written to
Const EXCEL_WORKBOOK = "C:\Users\Bobby\Documents\Bobby.xlsx"
'--> On the next line edit the path to the Outlook folder containing the messages to be exported
Const OUTLOOK_FOLDER = "Mailbox - Bobby\Inbox\New Hires"
Const NOTE_NAME = "Export Messages to Excel - Last Run"
Const MACRO_NAME = "Export Messages to Excel (Rev 39)"

Sub ExportMessagesToExcel()
    Dim olkFld As Object, _
        olkMsg As Object, _
        olkNot As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Long, _
        lngCnt As Long, _
        strFil As String, _
        strTmp As String, _
        arrLin As Variant, _
        varLin As Variant, _
        datLst As Date
    On Error Resume Next
    Set olkNot = Session.GetDefaultFolder(olFolderNotes).Items.Item(NOTE_NAME)
    On Error GoTo 0
    If TypeName(olkNot) = "Nothing" Then
        Set olkNot = Application.CreateItem(olNoteItem)
        olkNot.Body = NOTE_NAME & vbCrLf & #1/1/2000 12:01:00 AM#
        olkNot.Save
    End If
    datLst = Replace(olkNot.Body, NOTE_NAME & vbCrLf, "")
    Set olkFld = OpenOutlookFolder(OUTLOOK_FOLDER).Items.Restrict("[ReceivedTime] > '" & Format(datLst, "ddddd h:nn AMPM") & "'")
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(EXCEL_WORKBOOK)
    Set excWks = excWkb.Worksheets(1)
    lngRow = excWks.UsedRange.rows.Count + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            arrLin = Split(olkMsg.Body, vbCrLf)
            For Each varLin In arrLin
                strTmp = Trim(varLin)
                If Left(strTmp, 14) = "Employee Name:" Then
                    excWks.Cells(lngRow, 1) = Trim(Mid(strTmp, 15))
                Else
                    If Left(strTmp, 11) = "Start Date:" Then
                        excWks.Cells(lngRow, 2) = Trim(Mid(strTmp, 12))
                    Else
                        If Left(strTmp, 14) = "Work Location:" Then
                            excWks.Cells(lngRow, 3) = Trim(Mid(strTmp, 15))
                        End If
                    End If
                End If
            Next
            lngRow = lngRow + 1
            lngCnt = lngCnt + 1
        End If
    Next
    olkNot.Body = NOTE_NAME & vbCrLf & Now
    olkNot.Save
    Set olkNot = Nothing
    Set olkFld = Nothing
    Set olkMsg = Nothing
    excWks.Columns("A:C").AutoFit
    excWkb.Close True
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    ' Purpose: Opens an Outlook folder from a folder path.
    ' Written: 4/24/2009
    ' Author:  David Lee
    ' Outlook: All versions
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        bolBeyondRoot As Boolean
    On Error Resume Next
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        Do While Left(strFolderPath, 1) = "\"
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        Loop
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            Select Case bolBeyondRoot
                Case False
                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)
                    bolBeyondRoot = True
                Case True
                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)
            End Select
            If Err.Number <> 0 Then
                Set OpenOutlookFolder = Nothing
                Exit For
            End If
        Next
    End If
    On Error GoTo 0
End Function
 

I put this revision together for a reader named chanakkiyan. Chanakkiyan asked for a version that merges Rev9′s ability to export the entire mailbox with Rev34′s ability to save and hyperlink to the attachments of each exported message.

Chanakkiyan will use the instructions from the original post to add this code to Outlook.

'--> Declare some constants
'On the next line, edit the path on the next line.  This is the folder the code will save the attachments to.  Make sure the path ends with a \
Const EXPORT_FOLDER = "C:\Users\David\Documents\TestArea\chanakkiyan\files\"
'On the next line, edit the path to the spreadsheet you want to export to.
Const EXCEL_WORKSHEET = "C:\Users\David\Documents\TestArea\chanakkiyan\chanakkiyan.xlsx"
Const MACRO_NAME = "Export Messages to Excel (Rev 40)"
 
Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    objFSO As Object, _
    intVer As Integer, _
    lngRow As Long

Sub ExportController()
    Dim olkSto As Object, _
        olkFld As Object

    '--> Initialize some variables
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    intVer = GetOutlookVersion()
    
    '--> Connect to Excel
    Set excApp = CreateObject("Excel.Application")
    Set excWkb = excApp.Workbooks.Open(EXCEL_WORKSHEET)
    Set excWks = excWkb.Worksheets(1)
    lngRow = excWks.UsedRange.rows.Count + 1
    
    '--> Main routine
    For Each olkSto In Session.Stores
        Set olkFld = olkSto.GetRootFolder
        ExportMessagesToExcel olkFld
    Next
    excWks.Columns("A:ZZ").AutoFit
    
    '--> Save and close the workbook
    excWkb.Close True
    
    '--> Destroy all objects
    Set olkSto = Nothing
    Set olkFld = Nothing
    Set objFSO = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    
    '--> Notify the user that the process is finished
    MsgBox "Export complete.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Sub ExportMessagesToExcel(olkFld As Outlook.MAPIFolder)
    '--> Declare some variables
    Dim olkMsg As Object, _
        olkSub As Object, _
        olkAtt As Object, _
        lngCol As Long, _
        strAFN As String, _
        arrTmp As Variant
         
    '--> Main routine
    'Only process folders containing emails, not contacts, calendars, tasks, or notes.
    If olkFld.DefaultItemType = olMailItem Then
        For Each olkMsg In olkFld.Items
            If olkMsg.Class = olMail Then
                lngCol = 5
                'Add a row for each field in the message you want to export
                excWks.Cells(lngRow, 1) = olkFld.FolderPath
                excWks.Cells(lngRow, 2) = olkMsg.Subject
                excWks.Cells(lngRow, 3) = olkMsg.ReceivedTime
                excWks.Cells(lngRow, 4) = GetSMTPAddress(olkMsg, intVer)
                For Each olkAtt In olkMsg.Attachments
                    If (Not IsHiddenAttachment(olkAtt)) And (Not olkAtt.Type = olOLE) Then
                        strAFN = EXPORT_FOLDER & objFSO.GetBaseName(olkAtt.Filename) & Format(olkMsg.ReceivedTime, "yyyy-mm-dd-hh-nn-ss") & "." & objFSO.GetExtensionName(olkAtt.Filename)
                        olkAtt.SaveAsFile strAFN
                        excWks.Hyperlinks.Add Anchor:=excWks.Range(NumberToLetter(lngCol) & lngRow), Address:="FILE:\\" & strAFN, TextToDisplay:=olkAtt.Filename
                        lngCol = lngCol + 1
                    End If
                Next
                lngRow = lngRow + 1
            End If
        Next
        'Process all the subfolders under this folder
        For Each olkSub In olkFld.Folders
            ExportMessagesToExcel olkSub
        Next
    End If

    '--> Destroy all objects
    Set olkMsg = Nothing
    Set olkSub = Nothing
    Set olkAtt = Nothing
End Sub
 
Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function
 
Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function
 
Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function
 
Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function
 
Private Function NumberToLetter(ByVal intNumber As Integer) As String
    NumberToLetter = Chr(64 + intNumber)
End Function

I put this revision together for a reader named Singh. Singh has multiple mailboxes and wants to be able to export all the messages from a given mailbox. In rev 3 I showed how to export all the messages from a folder and all it’s subfolders, while in rev 9 I showed how to export messages from every store (mailbox or PST file) in Outlook. In this revision I’ve modified the code from rev 3 to handle just one store (mailbox or PST file).

Singh will use the instructions from the original post to add this code to Outlook.

'On the next line edit the name of the store (mailbox) you want to export
Const STORE_NAME = "Mailbox - Singh"
Const MACRO_NAME = "Export Messages to Excel (Rev 41)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVer As Integer, _
    intCnt As Integer

Sub ExportMessagesToExcel()
    Dim strFil As String
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        intCnt = 0
        intVer = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
        End With
        ProcessFolder GetRootFolder(STORE_NAME)
        excWkb.SaveAs strFil
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder, intRow As Integer
    intRow = excWks.UsedRange.rows.Count
    intRow = intRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVer)
            intRow = intRow + 1
            intCnt = intCnt + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkssub = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function GetRootFolder(strStoreName As String) As Outlook.MAPIFolder
    Dim olkSto As Outlook.Store
    On Error Resume Next
    If strStoreName = "" Then
        Set GetRootFolder = Nothing
    Else
        Set olkSto = Session.Stores.Item(strStoreName)
        If TypeName(olkSto) <> "Nothing" Then
            Set GetRootFolder = olkSto.GetRootFolder
        End If
    End If
    On Error GoTo 0
    Set olkSto = Nothing
End Function

This revision is for David G., a reader who needs to export all the messages in the currently selected folder and all of its sub-folders. For each message, David wants to export the subject, date received, the sender, who the message was sent to, a list of the message’s attachments, who the message was CC’d to, the message body, and the message’s internet header. This solution will only work in Outlook 2007 and later.

David G. will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 42)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVer As Integer, _
    intCnt As Integer

Sub ExportMessagesToExcel()
    Dim strFil As String
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        intCnt = 0
        intVer = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Date"
            .Cells(1, 3) = "From"
            .Cells(1, 4) = "To"
            .Cells(1, 5) = "Attachment"
            .Cells(1, 6) = "CC"
            .Cells(1, 7) = "Body"
            .Cells(1, 8) = "Header"
        End With
        ProcessFolder Application.ActiveExplorer.CurrentFolder
        excWks.Columns("A:I").AutoFit
        excWkb.SaveAs strFil
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, _
        olkAtt As Object, _
        olkSub As Object, _
        intRow As Integer, _
        strAtt As String
    intRow = excWks.UsedRange.rows.Count
    intRow = intRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Get the names of all attachments
            strAtt = ""
            For Each olkAtt In olkMsg.Attachments
                If Not IsHiddenAttachment(olkAtt) And olkAtt.Type <> olOLE Then
                    strAtt = strAtt & olkAtt.Filename & ", "
                End If
            Next
            If Len(strAtt) > 0 Then
                strAtt = Left(strAtt, Len(strAtt) - 2)
            End If
            'Add a row for each field in the message you want to export
            With excWks
                .Cells(intRow, 1) = olkMsg.Subject
                .Cells(intRow, 2) = olkMsg.ReceivedTime
                .Cells(intRow, 3) = GetSMTPAddress(olkMsg, intVer)
                .Cells(intRow, 4) = olkMsg.To
                .Cells(intRow, 5) = strAtt
                .Cells(intRow, 6) = olkMsg.CC
                .Cells(intRow, 7) = olkMsg.Body
                .Cells(intRow, 8) = GetInetHeaders(olkMsg)
            End With
            intRow = intRow + 1
            intCnt = intCnt + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkSub = Nothing
    Set olkMsg = Nothing
    Set olkAtt = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function GetInetHeaders(olkMsg As Outlook.MailItem) As String
    ' Purpose: Returns the internet headers of a message.'
    ' Written: 4/28/2009'
    ' Author:  David Lee'
    ' Outlook: 2007'
    Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
    Dim olkPA As Outlook.PropertyAccessor
    Set olkPA = olkMsg.PropertyAccessor
    GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
    Set olkPA = Nothing
End Function

Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    ' Purpose: Determines if an attachment is a hidden attachment.
    ' Written: 7/12/2012
    ' Author:  David Lee
    ' Outlook: 2007 and later
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

This revision is for Abdul who needs to export data from messages containing a 2×4 table. Each message will contain just one table in the message body. The table’s first column contains labels, the second contains the actual data. This solution is a knock-off of the solution I first introduced in revision 27.

Abdul will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 43)"

Private Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        intRow As Integer, _
        intCnt As Integer, _
        strFil As String, _
        arrCel As Variant
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Name"
            .Cells(1, 2) = "ID"
            .Cells(1, 3) = "Education"
            .Cells(1, 4) = "State"
        End With
        intRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Get the cells from the table in the body of the message
                arrCel = Split(GetCells(olkMsg.HTMLBody), Chr(255))
                'Add a row for each field in the message you want to export
                excWks.Cells(intRow, 1) = arrCel(1)
                excWks.Cells(intRow, 2) = arrCel(3)
                excWks.Cells(intRow, 3) = arrCel(5)
                excWks.Cells(intRow, 4) = arrCel(7)
                intRow = intRow + 1
                intCnt = intCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFil
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetCells(strHTML As String) As String
    Const READYSTATE_COMPLETE = 4
    Dim objIE As Object, objDoc As Object, colCells As Object, objCell As Object
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.navigate "about:blank"
    Do Until objIE.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop
    objIE.document.Body.innerHTML = strHTML
    Set objDoc = objIE.document
    Set colCells = objDoc.getElementsByTagName("td")
    If colCells.Length > 0 Then
        For Each objCell In colCells
            GetCells = GetCells & objCell.innerText & Chr(255)
        Next
        GetCells = Left(GetCells, Len(GetCells) - 1)
    Else
        GetCells = ""
    End If
    Set objCell = Nothing
    Set colCells = Nothing
    Set objDoc = Nothing
    Set objIE = Nothing
End Function

This revision is for Wayne who needs to parse three pieces of data from the body of certain messages. This solution is a knock-off of the solution I first introduced in revision 14.

Wayne will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 44)"

Sub ExportMessagesToExcel()
    Dim olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Integer, _
        lngCnt As Long, _
        strFil As String, _
        strTmp As String, _
        arrLin As Variant, _
        varLin As Variant
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Received"
            .Cells(1, 2) = "Name"
            .Cells(1, 3) = "Contact No"
            .Cells(1, 4) = "Email Address"
        End With
        lngRow = 2
        'Write messages to spreadsheet
        For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(lngRow, 1) = olkMsg.ReceivedTime
                arrLin = Split(olkMsg.Body, vbCrLf)
                For Each varLin In arrLin
                    strTmp = Trim(varLin)
                    If Left(strTmp, 6) = "Name: " Then
                        excWks.Cells(lngRow, 2) = Mid(strTmp, 7)
                    Else
                        If Left(strTmp, 12) = "Contact No: " Then
                            excWks.Cells(lngRow, 3) = Mid(strTmp, 13)
                        Else
                            If Left(strTmp, 15) = "Email Address: " Then
                                excWks.Cells(lngRow, 4) = Mid(strTmp, 16)
                            End If
                        End If
                    End If
                Next
                lngRow = lngRow + 1
                lngCnt = lngCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFil
        excWkb.Close
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

This revision is for Lin who needs to export some basic information (e.g. subject, sender, date received) plus the message body, up to a certain phrase, for all message received in a certain time-frame. The solution is a knock-off of the solution from revision 4 that adds the ability to extract the body of the message up to a designated key phrase. In Lin’s case, that key phrase is the beginning of a disclaimer that appears in each message.

Lin will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 45)"
 
Sub ExportMessagesToExcel()
    Dim olkLst As Object, _
        olkMsg As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Long, _
        lngCnt As Long, _
        intVer As Integer, _
        strFil As String, _
        strRng As String, _
        arrTmp As Variant, _
        datBeg As Date, _
        datEnd As Date
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        strRng = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
        arrTmp = Split(strRng, "to")
        datBeg = IIf(IsDate(arrTmp(0)), arrTmp(0), Date) & " 12:00am"
        datEnd = IIf(IsDate(arrTmp(1)), arrTmp(1), Date) & " 11:59pm"
        intVer = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add()
        Set excWks = excWkb.ActiveSheet
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Subject"
            .Cells(1, 2) = "Received"
            .Cells(1, 3) = "Sender"
            .Cells(1, 4) = "Body"
        End With
        lngRow = 2
        'Write messages to spreadsheet
        Set olkLst = Application.ActiveExplorer.CurrentFolder.Items.Restrict("[ReceivedTime] >= '" & Format(datBeg, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
        For Each olkMsg In olkLst
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                'Add a row for each field in the message you want to export
                excWks.Cells(lngRow, 1) = olkMsg.Subject
                excWks.Cells(lngRow, 2) = olkMsg.ReceivedTime
                excWks.Cells(lngRow, 3) = GetSMTPAddress(olkMsg, intVer)
                excWks.Cells(lngRow, 4) = GetMsgBody(olkMsg.Body)
                lngRow = lngRow + 1
                lngCnt = lngCnt + 1
            End If
        Next
        Set olkMsg = Nothing
        excWkb.SaveAs strFil
        excWkb.Close
    End If
    Set olkLst = Nothing
    Set excWks = Nothing
    Set excWkb = Nothing
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub
 
Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function
 
Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function
 
Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Function GetMsgBody(strTxt As String) As String
    Const TEXT_TO_FIND = "The information contained in this message is intended only for the recipient, and may be a confidential"
    Dim lngPos As Long
    lngPos = InStr(1, strTxt, TEXT_TO_FIND, vbTextCompare)
    If lngPos > 0 Then
        GetMsgBody = Left(strTxt, lngPos - 1)
    Else
        GetMsgBody = strTxt
    End If
End Function

I put this revision together for Jörg who asked for a knock-off of rev 42 with a few additions. Specifically

  1. Adds the BCC recipients (will only show up when he runs it against a folder of sent items)
  2. The path to the folder the message is in
  3. A count of the number of recipients the message is addressed to
  4. The number of attachments
  5. Split the time received into two columns with the date in one and the time in the other

Jörg will use the instructions from the original post to add this code to Outlook.

Const MACRO_NAME = "Export Messages to Excel (Rev 46)"

Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVer As Integer, _
    intCnt As Integer

Sub ExportMessagesToExcel()
    Dim strFil As String
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
    If strFil <> "" Then
        intCnt = 0
        intVer = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        'Write Excel Column Headers
        With excWks
            .Cells(1, 1) = "Folder"
            .Cells(1, 2) = "Subject"
            .Cells(1, 3) = "Date"
            .Cells(1, 4) = "Time"
            .Cells(1, 5) = "From"
            .Cells(1, 6) = "To"
            .Cells(1, 7) = "Attachment"
            .Cells(1, 8) = "CC"
            .Cells(1, 9) = "BCC"
            .Cells(1, 10) = "Body"
            .Cells(1, 11) = "Header"
            .Cells(1, 12) = "Recipients"
            .Cells(1, 13) = "Attachments"
        End With
        ProcessFolder Application.ActiveExplorer.CurrentFolder
        excWks.Columns("A:I").AutoFit
        excWks.Columns("K:M").AutoFit
        excWkb.SaveAs strFil
    End If
    Set excWks = Nothing
    Set excWkb = Nothing
    excApp.Quit
    Set excApp = Nothing
    MsgBox "Process complete.  A total of " & intCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, _
        olkAtt As Object, _
        olkSub As Object, _
        intRow As Integer, _
        strAtt As String
    intRow = excWks.UsedRange.rows.Count
    intRow = intRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            Debug.Print olkMsg.Subject
            'Get the names of all attachments
            strAtt = ""
            For Each olkAtt In olkMsg.Attachments
                If Not IsHiddenAttachment(olkAtt) And olkAtt.Type <> olOLE Then
                    strAtt = strAtt & olkAtt.Filename & ", "
                End If
            Next
            If Len(strAtt) > 0 Then
                strAtt = Left(strAtt, Len(strAtt) - 2)
            End If
            'Add a row for each field in the message you want to export
            With excWks
                .Cells(intRow, 1) = olkMsg.Parent.FolderPath
                .Cells(intRow, 2) = olkMsg.Subject
                .Cells(intRow, 3) = Format(olkMsg.ReceivedTime, "yymmdd")
                .Cells(intRow, 4) = Format(olkMsg.ReceivedTime, "hh:nn")
                .Cells(intRow, 5) = GetSMTPAddress(olkMsg, intVer)
                .Cells(intRow, 6) = olkMsg.To
                .Cells(intRow, 7) = strAtt
                .Cells(intRow, 8) = olkMsg.CC
                .Cells(intRow, 9) = olkMsg.BCC
                .Cells(intRow, 10) = olkMsg.Body
                .Cells(intRow, 11) = GetInetHeaders(olkMsg)
                .Cells(intRow, 12) = olkMsg.Recipients.Count
                .Cells(intRow, 13) = olkMsg.Attachments.Count
            End With
            intRow = intRow + 1
            intCnt = intCnt + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkSub = Nothing
    Set olkMsg = Nothing
    Set olkAtt = Nothing
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function GetInetHeaders(olkMsg As Outlook.MailItem) As String
    ' Purpose: Returns the internet headers of a message.'
    ' Written: 4/28/2009'
    ' Author:  David Lee'
    ' Outlook: 2007'
    Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
    Dim olkPA As Outlook.PropertyAccessor
    Set olkPA = olkMsg.PropertyAccessor
    GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
    Set olkPA = Nothing
End Function

Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
    ' Purpose: Determines if an attachment is a hidden attachment.
    ' Written: 7/12/2012
    ' Author:  David Lee
    ' Outlook: 2007 and later
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
    On Error Resume Next
    Set olkPA = olkAtt.PropertyAccessor
    varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
    IsHiddenAttachment = (varTemp <> "")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for nbnamp, a reader who asked for a mashup of features from revs 3, 7, and 23. In nbnamp’s own words

Is there a way to mix rev.3, rev.7, and rev.23?
From rev.3: export emails from folder and its subfolders
From rev7: the data to be added to the bottom of an existing spreadsheet
And from rev.23: connect to an already open spreadsheet

This revision does all of those things.

Nbnamp will follow the instructions from the original post to add this code to Outlook.

'On the next line edit the name of the workbook
Const WBK_NAME = "Book1"
'On the next line edit the name of the worksheet
Const WKS_NAME = "Sheet1"
Const MACRO_NAME = "Export Messages to Excel (Rev 47)"
 
Dim excApp As Object, _
    excWkb As Object, _
    excWks As Object, _
    intVer As Integer, _
    lngMsg As Long
 
Sub ExportMessagesToExcel()
    On Error Resume Next
    Set excApp = GetObject(, "Excel.Application")
    If TypeName(excApp) = "Nothing" Then
        MsgBox "Excel is not open.  Operation aborted.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        Set excWkb = excApp.Workbooks.Item(WBK_NAME)
        If TypeName(excWkb) = "Nothing" Then
            MsgBox "The workbook " & WBK_NAME & " is not open.  Operation aborted.", vbCritical + vbOKOnly, MACRO_NAME
        Else
            On Error GoTo 0
            lngMsg = 0
            intVer = GetOutlookVersion()
            Set excWks = excWkb.Worksheets(WKS_NAME)
            ProcessFolder Application.ActiveExplorer.CurrentFolder
            excWkb.Save
            Set excWks = Nothing
            Set excWkb = Nothing
            Set excApp = Nothing
            MsgBox "Process complete.  A total of " & lngMsg & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
        End If
    End If
    On Error GoTo 0
End Sub
 
Private Sub ProcessFolder(olkFld As Outlook.MAPIFolder)
    Dim olkMsg As Object, olkSub As Outlook.MAPIFolder, lngRow As Long
    lngRow = excWks.UsedRange.rows.Count
    lngRow = lngRow + 1
    'Write messages to spreadsheet
    For Each olkMsg In olkFld.Items
        'Only export messages, not receipts or appointment requests, etc.
        If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(lngRow, 1) = GetSMTPAddress(olkMsg, intVer)
            excWks.Cells(lngRow, 2) = olkMsg.To
            excWks.Cells(lngRow, 3) = olkMsg.ReceivedTime
            excWks.Cells(lngRow, 4) = olkMsg.Subject
            excWks.Cells(lngRow, 5) = olkMsg.Body
            lngRow = lngRow + 1
            lngMsg = lngMsg + 1
        End If
    Next
    Set olkMsg = Nothing
    For Each olkSub In olkFld.Folders
        ProcessFolder olkSub
    Next
    Set olkssub = Nothing
End Sub
 
Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function
 
Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function
 
Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

I put this revision together for Arun Kumar who needs to export data from the body of certain messages. The script writes the data to an existing spreadsheet and avoids exporting items twice by tagging processed items with a category.

Arun will use the instructions from the original post to add this code to Outlook.

'On the next line, edit the path to the workbook the data will be exported to
Const WORKBOOK_NAME = "c:\users\arun\documents\testarea\arun_kumar.xlsx"
'On the next line, edit the name of the category the script will assign to items that it has processed
Const CATEGORY_NAME = "Arun"
'On the next line, edit the path to the root folder the attachments are to be saved under
Const ROOT_DIR = "D:\Work"
Const MACRO_NAME = "Export Messages to Excel (Rev 48)"

Private Sub ExportMessagesToExcel()
    Dim olkFld As Object, _
        olkRng As Object, _
        olkMsg As Object, _
        olkAtt As Object, _
        objFSO As Object, _
        excApp As Object, _
        excWkb As Object, _
        excWks As Object, _
        lngRow As Long, _
        lngCnt As Long, _
        intVersion As Integer, _
        strTemp As String, _
        arrLines As Variant, _
        varLine As Variant, _
        strDat As String, _
        arrTmp As Variant, _
        datBeg As Date, _
        datEnd As Date, _
        strExt As String, _
        strDir As String, _
        strOrd As String
    'Write messages to spreadsheet
    strDat = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
    arrTmp = Split(strDat, "to")
    datBeg = IIf(IsDate(arrTmp(0)), arrTmp(0), Date) & " 12:00am"
    datEnd = IIf(IsDate(arrTmp(1)), arrTmp(1), Date) & " 11:59pm"
    Set olkFld = Application.ActiveExplorer.CurrentFolder
    Set olkRng = olkFld.Items.Restrict("[ReceivedTime] >= '" & Format(datBeg, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
    If olkRng.Count > 0 Then
        intVersion = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Open(WORKBOOK_NAME)
        Set excWks = excWkb.Worksheets(1)
        lngRow = excWks.UsedRange.rows.Count + 1
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        For Each olkMsg In olkRng
            'Only export messages, not receipts or appointment requests, etc.
            If olkMsg.Class = olMail Then
                If Not IsTagged(olkMsg.Categories) Then
                    'Add a row for each field in the message you want to export
                    excWks.Cells(lngRow, 1) = olkMsg.ReceivedTime
                    excWks.Cells(lngRow, 2) = GetSMTPAddress(olkMsg, intVersion)
                    arrLines = Split(olkMsg.Body, vbCrLf)
                    For Each varLine In arrLines
                        strTemp = Trim(varLine)
                        If Left(strTemp, 10) = "Order No: " Then
                            strOrd = Mid(strTemp, 11)
                            excWks.Cells(lngRow, 3) = Mid(strTemp, 11)
                        Else
                            If Left(strTemp, 9) = "Loan No: " Then
                                excWks.Cells(lngRow, 4) = Mid(strTemp, 10)
                            Else
                                If Left(strTemp, 15) = "Borrower Name: " Then
                                    excWks.Cells(lngRow, 5) = Mid(strTemp, 16)
                                    bolComments = True
                                Else
                                    If Left(strTemp, 18) = "Property Address: " Then
                                        excWks.Cells(lngRow, 6) = Mid(strTemp, 19)
                                    Else
                                        If Left(strTemp, 16) = "Vendor Product: " Then
                                            excWks.Cells(lngRow, 7) = Mid(strTemp, 17)
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    Next
                    'Save the Word attachment, if there is one
                    For Each olkAtt In olkMsg.Attachments
                        strExt = objFSO.GetExtensionName(olkAtt.Filename)
                        Select Case strExt
                            Case "doc", "docx"
                                strDir = ROOT_DIR & "\" & MonthName(Month(olkMsg.ReceivedTime), True)
                                If Not objFSO.FolderExists(strDir) Then objFSO.CreateFolder strDir
                                strDir = strDir & "\" & StrZero(Day(olkMsg.ReceivedTime), 2)
                                If Not objFSO.FolderExists(strDir) Then objFSO.CreateFolder strDir
                                strDir = strDir & "\" & strOrd
                                If Not objFSO.FolderExists(strDir) Then objFSO.CreateFolder strDir
                                olkAtt.SaveAsFile strDir & "\" & olkAtt.Filename
                        End Select
                    Next
                    'Tag the message so it isn't processed a second time
                    If Len(olkMsg.Categories) = 0 Then
                        olkMsg.Categories = CATEGORY_NAME
                    Else
                        olkMsg.Categories = olkMsg.Categories & "," & CATEGORY_NAME
                    End If
                    olkMsg.Save
                    lngRow = lngRow + 1
                    lngCnt = lngCnt + 1
                End If
            End If
        Next
        Set olkMsg = Nothing
        excWks.Columns("A:G").AutoFit
        excWkb.Close True
        Set excWks = Nothing
        Set excWkb = Nothing
        Set excApp = Nothing
    Else
        MsgBox "Operation cancelled.  There are no items in the range " & datBeg & " - " & datEnd, vbInformation + vbOKOnly, MACRO_NAME
    End If
    MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
End Sub

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Private Function IsTagged(strCats As String) As Boolean
    Dim arrCats As Variant, varItem As Variant
    arrCats = Split(strCats, ",")
    For Each varItem In arrCats
        If varItem = CATEGORY_NAME Then
            IsTagged = True
            Exit For
        End If
    Next
End Function

Private Function StrZero(varNumber As Variant, intLength As Integer) As String
    ' Purpose: Pad a number with zeroes to the given length and return it as a string.'
    ' Written: 4/24/2009'
    ' Author:  TechnicLee'
    ' Outlook: All versions'
    Dim intItemLength As Integer
    If IsNumeric(varNumber) Then
        intItemLength = Len(CStr(Int(varNumber)))
        If intItemLength < intLength Then
            StrZero = String(intLength - intItemLength, "0") & varNumber
        Else
            StrZero = varNumber
        End If
    Else
        StrZero = varNumber
    End If
End Function 

I put this revision together for Anders who needs to export all the email addresses from the bodies of certain messages. The script writes the sender’s address to column A and the harvested addresses to separate columns beginning in column B. The solution saves the data from each run to a new spreadsheet.

Anders will use the instructions from the original post to add this code to Outlook.

Sub ExportAddresses()
    Const MACRO_NAME = "Export Addresses"
    Dim olkItm As Object, strBuf As String, arrAdr As Variant, varAdr As Variant, lngRow As Long, lngCol As Long, strFil As String, intVer As Integer
    Dim excApp As Object, excWkb As Object, excWks As Object
    strFil = InputBox("Enter a filename (including path) to save the exported messages to.", "Export Messages to Excel")
    If strFil = "" Then
        MsgBox "You must enter the name of a file to save the output to.  Processing cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    Else
        intVer = GetOutlookVersion()
        Set excApp = CreateObject("Excel.Application")
        Set excWkb = excApp.Workbooks.Add
        Set excWks = excWkb.Worksheets(1)
        lngRow = 1
        For Each olkItm In Application.ActiveExplorer.CurrentFolder.Items
            If olkItm.Class = olMail Then
                strBuf = FindString(olkItm.Body, "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")
                If strBuf <> "Not found" Then
                    excWks.Cells(lngRow, 1) = GetSMTPAddress(olkItm, intVer)
                    lngCol = 2
                    arrAdr = Split(strBuf, "|")
                    For Each varAdr In arrAdr
                        excWks.Cells(lngRow, lngCol) = varAdr
                        lngCol = lngCol + 1
                    Next
                    excWks.Columns("A:" & NumberToLetter(lngCol)).AutoFit
                    lngRow = lngRow + 1
                End If
            End If
        Next
        Set excWks = Nothing
        excWkb.SaveAs strFil
        excWkb.Close False
        Set excWkb = Nothing
        Set excApp = Nothing
        Set olkItm = Nothing
        MsgBox "Processing complete.", vbInformation + vbOKOnly, MACRO_NAME
    End If
End Sub

Private Function FindString(strText As String, strFind As String) As String
    Dim objRegEx As Object, colMatches As Object, objMatch As Object
    Set objRegEx = CreateObject("VBscript.RegExp")
    With objRegEx
        .IgnoreCase = True
        .Global = True
        .Pattern = strFind
        Set colMatches = .Execute(strText)
    End With
    If colMatches.Count > 0 Then
        For Each objMatch In colMatches
            FindString = FindString & objMatch.Value & "|"
        Next
        FindString = Left(FindString, Len(FindString) - 1)
    Else
        FindString = "Not found"
    End If
    Set objRegEx = Nothing
    Set colMatches = Nothing
    Set objMatch = Nothing
End Function

Private Function NumberToLetter(ByVal intNumber As Integer) As String
    Dim intTmp As Integer
    If intNumber <= 26 Then
        NumberToLetter = Chr(64 + intNumber)
    Else
        NumberToLetter = Chr(64 + Int(intNumber / 26)) & Chr(64 + intNumber Mod 26)
    End If
End Function

Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
    Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
    On Error Resume Next
    Select Case intOutlookVersion
        Case Is < 14
            If Item.SenderEmailType = "EX" Then
                GetSMTPAddress = SMTP2007(Item)
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
        Case Else
            Set olkSnd = Item.Sender
            If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                Set olkEnt = olkSnd.GetExchangeUser
                GetSMTPAddress = olkEnt.PrimarySmtpAddress
            Else
                GetSMTPAddress = Item.SenderEmailAddress
            End If
    End Select
    On Error GoTo 0
    Set olkPrp = Nothing
    Set olkSnd = Nothing
    Set olkEnt = Nothing
End Function

Private Function GetOutlookVersion() As Integer
    Dim arrVer As Variant
    arrVer = Split(Outlook.Version, ".")
    GetOutlookVersion = arrVer(0)
End Function

Private Function SMTP2007(olkMsg As Outlook.MailItem) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

1,702 comments on “Exporting Outlook Messages to Excel

  1. Hello David,

    You are doing fantastic work!!

    I was wondering if you might be able to provide me with the code to do the criteria noted below. It will likely take quite a bit of coding, and accordingly, I am happy to make a “donation” for your invaluable time and effort. Pls. let me know what works for you.

    Thank you in advance for your assistance.
    MaldonadoCJ

    Criteria:

    1) Extract the message body of an email into a worksheet.
    2) Extract the message body of an attachment into the same worksheet but in a different column. However, if the attachment is anything other than an email (i.e. Excel worksheet, PDF file), copy the attachment to a separate worksheet with a hyperlink linking it to its corresponding entry on the main worksheet.
    3) Have the code run automatically each business day at a specific time but also give the user the ability to run the code “on demand” based on a date range.
    4) Prompt the user to chose whether they want to select a subfolder, folder or an entire mailbox for the extraction.
    5) After the extraction from the source folder, move all of the emails to a history folder, but also ensure the emails are sent to their corresponding folder and subfolder, similar to the structure in the source folder; into the history folder. All emails that could not be extracted would remain in the source folder.
    6) Create hyperlinks for each entry on the main worksheet to its corresponding email in the history folder.
    7) Automatically create a new workbook for each extraction and have it saved in a location specified by the user; ideally through a message box prompt.
    8) The columns in the main worksheet would consist of the following fields:
    -Subject
    -Date
    -Note ALL individuals listed in the “From” field in the email as there may be multiple emails threads in one email. I would need all of their names.
    -To
    -Attachment Names (note all attachment names as there may be multiple attachments in one email)
    -Name of ALL Attachment Senders (Critical to have all of their names given multiple emails threads in one email)
    -Body of Email Message
    -Body of Attachment Message (if an email)
    -Hyperlinks for emails

    • Hi, Carlos.

      Thanks!

      In requirement #2, what do you mean when you say “copy”? Copy it how?
      In requirement #6, I assume you mean you want a link to the original message. Correct?
      In requirement #8, the third bullet, are you saying that you want to parse all the names from each “From:” line in the message?
      In requirement #8, the sixth bullet, are you saying that you want to determine who sent each attachment and include their name in this column? For example, a group carries on a long discussion via email. At various time during that discussion Sally and Joe have added an attachment. On being exported you want the solution to note that Sally and Joe each added an attachment. Correct?

      In addition to my questions, I’ll add a couple of observations to consider.

      1. In my experience, Excel doesn’t do well with an entire message in a cell. Are you sure you want the message text from the message and any message attachments in the spreadsheet?
      2. The solution for this is going to run fairly slowly. That’s a result of the methods I’ll have to employ to accomplish some of the requirements.

      Can you share what it is you want to accomplish with this? If I knew that, then I might be able to suggest some alternatives that would work better.

  2. Hi David,

    The revision 1 code will not run on my outlook 2013 exchange account. I have a few lines that are red with errors.
    Lines 13, 23-29, 35, and 78.

    Any help would be appreciated.

    • Hi, Andrew.

      Something must have gone wrong when you copied and pasted the code into Outlook. The copy-and-paste must have picked up some HTML encoding or something like that. Delete the code you have now, then try again, only this time copy-and-paste to Notepad first and then to Outlook. That usually gets rid of the unwanted codes or encoding.

    • I tried this but still the same lines highlighted red. I edited the first line (line 13) to see the error. Compile error: expected:=
      It is highlighting the ‘= <' after excWkb and before span class.

    • Hi, Andrew.

      “… before span class.”

      Unless I’m not understanding, you’re seeing HTML tags in the copied code. They shouldn’t be there. That’s an example of the sort of problem I described. Something is happening when you’re copying. It’s not the code, it’s the copy process that’s causing the problem. If you like, I can email you the code as a .txt file.

    • David,

      This is how the line looks in outlook, is it correct? This is the 1st line with an error.

      Set excWkb = excApp.Workbooks.Add()

    • Andrew,

      Yes, that line is correct. I’ve just emailed you the code from rev 1. Please replace the code you have now with it and let’s see what happens. My guess is that the errors will go away.

  3. Hi i need help . I need a script which extracts information from the body like i get phone no, contact name ,email id . so i need it to me extracted into separate rows can that be possible
    THanks in advance

    • Hi, Pradeephs.

      Can you share a sample message body with me? I’ll need that in order to determine whether I can extract the data you want and, if I can, how to accomplish that.

    • Hi David Lee

      The mail format i receive is like this inside the body

      example :-
      PARTS DESCRIPTION
      Parts Needed :
      Year :
      Make :
      Model :

      ADDITIONAL INFORMATION

      Body Style :

      Details :

      “CONTACT INFORMATION

      Name : Adroson
      Address :
      City :
      State :
      Zip :
      Phone : 5466666444
      Email : ”

      In it while extracting i need name and phone no to be extracted in separate rows in excel and also can it be done only to unread mails
      need help with it.

      Thanks in advance

    • Hi, pradeephs.

      Do you want any data from the message (e.g. subject, sender, date received) or just the name and phone number from the body? Also, how do you want the spreadsheet laid out?

    • HI David Lee
      i need subject ,phone no and Name thats it .
      like in row subject and 2 row phone no , in 3rd row name can it be possible and if can it be done for only unread mail.

      example of spreadsheet :-

      SUBJECT NAME Phone no
      XXXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331
      xxxXXXX mark 87322331

      Thanks in advance .

    • Hi, pradeephs.

      I believe that the code below will give you what you want. I have not tested it myself. Please try it out and let me know if it does what you described.

      Const MACRO_NAME = "Export Messages to Excel (Rev 14)"
       
      Sub ExportMessagesToExcel()
          Dim olkMsg As Object, _
              excApp As Object, _
              excWkb As Object, _
              excWks As Object, _
              lngRow As Long, _
              lngCnt As Long, _
              strFil As String, _
              strTmp As String, _
              arrLin As Variant, _
              varLin As Variant
          strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
          If strFil <> "" Then
              Set excApp = CreateObject("Excel.Application")
              Set excWkb = excApp.Workbooks.Add()
              Set excWks = excWkb.ActiveSheet
              'Write Excel Column Headers
              With excWks
                  .Cells(1, 1) = "Subject"
                  .Cells(1, 2) = "Name"
                  .Cells(1, 3) = "Phone"
              End With
              lngRow = 2
              'Write messages to spreadsheet
              For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
                  'Only export messages, not receipts or appointment requests, etc.
                  If olkMsg.Class = olMail Then
                      If olkMsg.UnRead Then
                          'Add a row for each field in the message you want to export
                          excWks.Cells(lngRow, 1) = olkMsg.Subject
                          arrLin = Split(olkMsg.Body, vbCrLf)
                          For Each varLin In arrLin
                              strTmp = Trim(varLin)
                              If Left(strTmp, 5) = "Name:" Then
                                  excWks.Cells(lngRow, 2) = Trim(Mid(strTmp, 6))
                              Else
                                  If Left(strTmp, 6) = "Phone:" Then
                                      excWks.Cells(lngRow, 3) = Trim(Mid(strTmp, 7))
                                  End If
                              End If
                          Next
                          lngRow = lngRow + 1
                          lngCnt = lngCnt + 1
                      End If
                  End If
              Next
              Set olkMsg = Nothing
              excWkb.SaveAs strFil
              excWkb.Close False
          End If
          Set excWks = Nothing
          Set excWkb = Nothing
          Set excApp = Nothing
          MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
      End Sub
      
    • Hi David Lee
      i tested the code but its not working the phone No and the Name will be inside the body its not updating to excel . can you please help me
      Thanks in advance

    • pradeephs,

      That’s hard to do without having a sample message to work from. If the messages are like what you described, then that code should work. Since it’s not working, I have to assume that the message body is different from the description. Can you share a sample message with me? If you can, then I’ll email you and you can reply with the sample message attached. Note that screen shots of the message, cutting and pasting, or forwarding won’t help. All those methods lose some of the original formatting.

    • Pradeep,

      Please try this version.

      Const MACRO_NAME = "Export Messages to Excel (Rev 14)"
        
      Sub ExportMessagesToExcel()
          Dim olkMsg As Object, _
              excApp As Object, _
              excWkb As Object, _
              excWks As Object, _
              lngRow As Long, _
              lngCnt As Long, _
              strFil As String, _
              strTmp As String, _
              arrLin As Variant, _
              varLin As Variant
          strFil = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
          If strFil <> "" Then
              Set excApp = CreateObject("Excel.Application")
              Set excWkb = excApp.Workbooks.Add()
              Set excWks = excWkb.ActiveSheet
              'Write Excel Column Headers
              With excWks
                  .Cells(1, 1) = "Subject"
                  .Cells(1, 2) = "Name"
                  .Cells(1, 3) = "Phone"
              End With
              lngRow = 2
              'Write messages to spreadsheet
              For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
                  'Only export messages, not receipts or appointment requests, etc.
                  If olkMsg.Class = olMail Then
                      If olkMsg.UnRead Then
                          'Add a row for each field in the message you want to export
                          excWks.Cells(lngRow, 1) = olkMsg.Subject
                          arrLin = Split(olkMsg.Body, vbCrLf)
                          For Each varLin In arrLin
                              strTmp = Trim(varLin)
                              If Left(strTmp, 6) = "Name :" Then
                                  excWks.Cells(lngRow, 2) = Trim(Mid(strTmp, 7))
                              Else
                                  If Left(strTmp, 7) = "Phone :" Then
                                      excWks.Cells(lngRow, 3) = Trim(Mid(strTmp, 8))
                                  End If
                              End If
                          Next
                          lngRow = lngRow + 1
                          lngCnt = lngCnt + 1
                      End If
                  End If
              Next
              Set olkMsg = Nothing
              excWkb.SaveAs strFil
              excWkb.Close False
          End If
          Set excWks = Nothing
          Set excWkb = Nothing
          Set excApp = Nothing
          MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
      End Sub
      
    • Hi David Lee

      Thanks for the help its working perfectly and a thanks a ton for your time and help ….

      it really help me a lot .

  4. This post has been a lifesaver. However I am having trouble piecing together a code for what i need it to do. I need to be able to export emails into a spreadsheet. they are located in a folder with sub-folders. I would like it to include the body( if the portion below could be done with the 7 lines then I would need line 8 until very end of message to be the body portion) of the email, subject, received date, sender. I would also like to have a column that takes the first seven lines and creates a column for each of the seven lines. I also apply categories to the emails as well. I need those to show up as a column as well. I just need the text from the categories assigned to show up. Can you advise? Thank you!

    • Hi, Veronica.

      Great! I’m glad the post has been helpful.

      Is there some way I can see a sample of one of these messages? If you can share one, then I can send you an email and you can reply with the sample attached.

  5. Just like to say first off, Thanks for sharing your knowledge…Thank-you
    The question is in to regards of REV-48. i would like to be able to take part of the body as the example below

    Name:
    John Dow

    Colour:
    Blue

    and so on. please if you could help that would be great as this is the way my emails are coming in to me
    thanks in advance

    • Hi, Johnson.

      You’re welcome!

      Do the values actually fall on the next line below the label or are they on the same line?

  6. hi – thanks for the wonderful script. i have one query on the sender details not displayed in the output – except for mail from external parties and only happens to internal mails i.e within our Exchange domain. how do i go about this? thks in advance

  7. Hello.Great guide and u been very helpfull.Well i have some questions i have one folder where i keep mails from a specifig group and i want to keep some statistics about them.For example when did i send my mail and when did the reply to me.Or when was the first mail i ever send to them based on the conversation in mail and when was the last reply.I hope i explained it well.If not tell here is an example
    His reply
    from :asdadda@yahoo.gr
    To :me@yahoo.gr
    CC :
    Subject :RE:hello
    date 4/4/2014
    Body : hello there
    My original mail
    from :me@yahoo.gr
    To :asdadda@yahoo.gr
    CC :
    Subject :hello
    Date 3/4/2014
    Body :hi m8.
    and i want to have somethink like
    Subject/body/from/to/date(date of last reply)/date(from original mail)
    Is that possible?thanks for your time.

    • I want the following fields :
      From/To/Subject/Date (from the first mail )and the same think from the replyed(as i stated above).Sorry for replying late but i was off for weekend.

    • Hello again,did u had time to see my case?I don’t want to sound bad asking again ,but i’m in big need.

    • Hi, Panos.

      I apologize for taking so long to respond to you. I read back over your request and am now concerned that I may have misunderstood what you want. My initial take was that you just wanted to export the details of individual messages. After re-reading your request I’m now thinking that what you want is to parse what amounts to the conversation history out of each message. In other words, the example you gave (below) is the conversation in a single message not individual messages. Is that correct? If so, then this is still doable, but the approach I need to take is very different.

      from :asdadda@yahoo.gr
      To :me@yahoo.gr
      CC :
      Subject :RE:hello
      date 4/4/2014
      Body : hello there
      My original mail
      from :me@yahoo.gr
      To :asdadda@yahoo.gr
      CC :
      Subject :hello
      Date 3/4/2014
      Body :hi m8.
      and i want to have somethink like
      Subject/body/from/to/date(date of last reply)/date(from original mail)
      Is that possible?thanks for your time.
      
    • Goodmorning from greece .Thanks again for your reply.You don’t need to apologize.My example is from one mail (but all the mails i’m interest to do that has the same stracture)so myabe how i put it was wrong.Let my self explain it again (i hope i do it proper this time).I work in a company and we are using outlook 2010 so we keep folders from mails from our clients.When we get their messages we reply to them when we have the product.So we want to do some statistics about that for example how many of the mails were about rma,how many about information etc.So far we do it manually where is a pain ….Cause they mails are like 1.5k a week.I tryed to do extract in accesss and it keeps all the details except the date of the customer send us the mail.So i’m looking for a method to keep the details from the message .The details i need are (from/to /date send /subject/body(body is not important if it is hard) from the reply we give and the same fields from the mail our client send it to us.I hope this helps better.Thanks again for your time

    • Good morning, Panos.

      I understand which details you want. What I am not clear on is whether the replies are in the same message as the original or whether each original and each reply are in separate messages. If they are in separate messages, then this is very easy. If they are in the same message, then doing this is a little bit more difficult. The answer to this question also determines the approach the solution needs to take. Also, does the solution need to tie replies to the original message (i.e. this message is a reply to this original) or is each message independent (i.e. this message is a reply, but it is immaterial which original message it is a reply to)?

    • Thanks again for the quick reply.The replies are always on the same message (like the example i stated)I know it’s hard but i don’t have any expirience with VMA and i tryed some of your solutions and the only think i can’t get was the date of the orginal message.(original i mean the first mail)

    • Hi, Panos.

      Here’s the code for doing what you described. I tested it against some messages I have and it appears to work correctly. Please try it out and let me know if it does what you want. Please follow the instructions from my original blog post add the code to Outlook.

      Sub ExportMessagesToExcel()
          Dim olkMsg As Object, _
              excApp As Object, _
              excWkb As Object, _
              excWks As Object, _
              lngRow As Long, _
              lngCnt As Long, _
              strFil As String, _
              strDat As String, _
              arrLin As Variant, _
              varLin As Variant
          strFil = InputBox("Enter a filename (including path) to save the exported messages to.", "Export Messages to Excel")
          If strFil <> "" Then
              Set excApp = CreateObject("Excel.Application")
              Set excWkb = excApp.Workbooks.Add()
              Set excWks = excWkb.ActiveSheet
              'Write Excel Column Headers
              With excWks
                  .Cells(1, 1) = "From"
                  .Cells(1, 2) = "To"
                  .Cells(1, 3) = "Subject"
                  .Cells(1, 4) = "Date"
              End With
              lngRow = 2
              'Write messages to spreadsheet
              For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
                  'Only export messages, not receipts or appointment requests, etc.
                  If olkMsg.Class = olMail Then
                      'Add a row for each field in the message you want to export
                      excWks.Cells(lngRow, 1) = olkMsg.SenderName
                      excWks.Cells(lngRow, 2) = olkMsg.To
                      excWks.Cells(lngRow, 3) = olkMsg.Subject
                      excWks.Cells(lngRow, 4) = olkMsg.ReceivedTime
                      lngRow = lngRow + 1
                      arrLin = Split(olkMsg.Body, vbCrLf)
                      For Each varLin In arrLin
                          If Left(varLin, 5) = "From:" Then
                              excWks.Cells(lngRow, 1) = Trim(Mid(varLin, 6))
                          Else
                              If Left(varLin, 5) = "Sent:" Then
                                  strDat = Trim(Mid(varLin, 6))
                                  strDat = StripDay(strDat)
                                  excWks.Cells(lngRow, 4) = CDate(strDat)
                              Else
                                  If Left(varLin, 3) = "To:" Then
                                      excWks.Cells(lngRow, 2) = Trim(Mid(varLin, 4))
                                  Else
                                      If Left(varLin, 8) = "Subject:" Then
                                          excWks.Cells(lngRow, 3) = Trim(Mid(varLin, 9))
                                          lngRow = lngRow + 1
                                      End If
                                  End If
                              End If
                          End If
                      Next
                      lngCnt = lngCnt + 1
                  End If
              Next
              Set olkMsg = Nothing
              excWks.Columns("A:D").AutoFit
              excWkb.SaveAs strFil
              excWkb.Close
          End If
          Set excWks = Nothing
          Set excWkb = Nothing
          Set excApp = Nothing
          MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
      End Sub
      
      Function StripDay(strVal As String) As String
          Dim arrDay As Variant, varDay As Variant
          StripDay = strVal
          arrDay = Split("Sunday,|Monday,|Tuesday,|Wednesday,|Thursday,|Friday,|Saturday,", "|")
          For Each varDay In arrDay
              If InStr(1, StripDay, varDay) > 0 Then
                  StripDay = Trim(Replace(StripDay, varDay, ""))
                  Exit For
              End If
          Next
      End Function
      
  8. Hello, Is it possible to change the revision 40 including column “To”, “From”, “CC” and “Email Body”? Is it possible instead of exporting attachments export all email from outlook(.msg) and make a hyperlink on the spreadsheet? Could you include start date and end date in this revision?

    • Hi, Vagston.

      That sounds a lot like revision 2. Please have a look at it. I can add the otehr fields you listed that it’s not already exporting.

    • Hello David Lee, tks for your answer. But How about to include (revision 2) the date range to export emails with specific dates?

    • Sry. Today I’m using revision 29(not revision 2), but How about to include The date range to export emails with specific dates? And change in the code “Application.ActiveExplorer.CurrentFolder” to an specific folder into outlook? Like Search folder with name: “All Documents”

    • Hi, Vagston.

      I’ve made changes to the code from revision 29 to handle a date range and to allow you to specify a search folder rather than the currently selected folder as the folder to process. I have not tested these changes. Please try this version and let me know if it’s what you wanted.

      '--> Declare some constants
      'On the next line edit the path to the folder you want to export the messages to.  The macro will create a sub-folder under this folder for each message.
      Const ROOT_PATH = "C:\Users\David\Documents\TestArea\JCR"
      Const MACRO_NAME = "Export Messages to Excel (Rev 29)"
       
      '--> Declare some variables
      Private excApp As Object, _
              excWkb As Object, _
              excWks As Object, _
              objFSO As Object, _
              lngCnt As Long, _
              lngRow As Long, _
              datStart As Date, _
              datEnd As Date
       
      Sub RunExportMessagesToExcel()
          '--> Declare some variables
          Dim strWkb As String, strDateRange As String, arrTemp As Variant
          strWkb = InputBox("Enter a filename (including path) to save the exported messages to.", MACRO_NAME)
          If strWkb <> "" Then
              strDateRange = InputBox("Enter the date range of the messages to export in the form ""mm/dd/yyyy to mm/dd/yyyy""", MACRO_NAME, Date & " to " & Date)
              arrTemp = Split(strDateRange, "to")
              datStart = IIf(IsDate(arrTemp(0)), arrTemp(0), Date) & " 12:00am"
              datEnd = IIf(IsDate(arrTemp(1)), arrTemp(1), Date) & " 11:59pm"
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              'Connect to Excel
              Set excApp = CreateObject("Excel.Application")
              Set excWkb = excApp.Workbooks.Add()
              Set excWks = excWkb.ActiveSheet
              'Write Excel Column Headers
              With excWks
                  .Cells(1, 1) = "Folder"
                  .Cells(1, 2) = "Subject"
                  .Cells(1, 3) = "To"
                  .Cells(1, 4) = "From"
                  .Cells(1, 5) = "CC"
                  .Cells(1, 6) = "Attachments"
                  .Cells(1, 7) = "ID"
                  .Cells(1, 8) = "Link"
              End With
              lngRow = 2
              'On the next line change "Store" to the name of the store the search folder is in and "Folder" to the name of the search folder you want
              ExportMessagesToExcel OpenOutlookSearchFolder("Store", "Folder")
              excWks.Columns("A:H").AutoFit
              excWkb.SaveAs strWkb
              excWkb.Close
              Set excWks = Nothing
              Set excWkb = Nothing
              Set excApp = Nothing
              Set objFSO = Nothing
              MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, MACRO_NAME
          Else
              MsgBox "Operation canceled.", vbInformation + vbOKOnly, MACRO_NAME
          End If
      End Sub
       
      Private Sub ExportMessagesToExcel(olkFol As Outlook.MAPIFolder)
          '--> Declare some variables
          Dim olkMsg As Object, _
              olkAtt As Object, _
              olkSub As Object, _
              objFol As Object, _
              olkLst As Object, _
              strAtt As String
          'Process the current folder and write its messages to the spreadsheet
          Set olkLst = olkFol.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
          'Write messages to spreadsheet
          For Each olkMsg In olkLst
              'Only export messages, not receipts or appointment requests, etc.
              If olkMsg.Class = olMail Then
                  strAtt = ""
                  'Create a new file system folder to store the message and its attachments
                  Set objFol = objFSO.CreateFolder(ROOT_PATH & "\" & olkMsg.EntryID)
                  'Save a plain-text version of the email to the folder
                  olkMsg.SaveAs objFol.Path & "\Message.txt", olTXT
                  'Save the attachments to the folder
                  For Each olkAtt In olkMsg.Attachments
                      If Not IsHiddenAttachment(olkAtt) Then
                          olkAtt.SaveAsFile objFol.Path & "\" & olkAtt.FileName
                          strAtt = strAtt & olkAtt.FileName & ", "
                      End If
                  Next
                  If Len(strAtt) > 0 Then strAtt = Left(strAtt, Len(strAtt) - 2)
                  'Add a row for each field in the message you want to export
                  excWks.Cells(lngRow, 1) = olkMsg.Parent.FolderPath
                  excWks.Cells(lngRow, 2) = olkMsg.Subject
                  excWks.Cells(lngRow, 3) = olkMsg.To
                  excWks.Cells(lngRow, 4) = olkMsg.SenderName
                  excWks.Cells(lngRow, 5) = olkMsg.CC
                  excWks.Cells(lngRow, 6) = strAtt
                  excWks.Cells(lngRow, 7) = olkMsg.EntryID
                  excWks.Hyperlinks.Add Anchor:=excWks.Range("H" & lngRow, "H" & lngRow), Address:="file://" & objFol.Path, TextToDisplay:="View Original Items"
                  lngRow = lngRow + 1
                  lngCnt = lngCnt + 1
                  Set olkAtt = Nothing
              End If
              DoEvents
          Next
          Set olkLst = Nothing
          Set olkMsg = Nothing
          Set objFol = Nothing
          For Each olkSub In olkFol.Folders
              ExportMessagesToExcel olkSub
              DoEvents
          Next
          Set olkSub = Nothing
      End Sub
       
      Private Function IsHiddenAttachment(olkAtt As Outlook.Attachment) As Boolean
          ' Purpose: Determines if an attachment is a hidden attachment.
          ' Written: 7/12/2012
          ' Author:  David Lee
          ' Outlook: 2007 and later
          Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
          Dim olkPA As Outlook.PropertyAccessor, varTemp As Variant
          On Error Resume Next
          Set olkPA = olkAtt.PropertyAccessor
          varTemp = olkPA.GetProperty(PR_ATTACH_CONTENT_ID)
          IsHiddenAttachment = (varTemp <> "")
          On Error GoTo 0
          Set olkPA = Nothing
      End Function
      
      Public Function OpenOutlookSearchFolder(strStore As String, strFolder As String) As Outlook.MAPIFolder
          ' Purpose: Opens an Outlook search folder by name.'
          ' Written: 4/10/2014'
          ' Author:  David Lee'
          ' Outlook: All versions'
          Dim olkStore As Object, _
              olkFolders As Object
          On Error Resume Next
          If (strStore = "") Or (strFolder = "") Then
              Set OpenOutlookSearchFolder = Nothing
          Else
              Set olkStore = Outlook.Session.Stores.Item(strStore)
              If TypeName(olkStore) = "Nothing" Then
                  OpenOutlookSearchFolder = Nothing
              Else
                  Set olkFolders = olkStore.GetSearchFolders
                  Set OpenOutlookSearchFolder = olkFolders.Item(strFolder)
                  If TypeName(OpenOutlookSearchFolder) = "Nothing" Then
                      OpenOutlookSearchFolder = Nothing
                  End If
              End If
          End If
          On Error GoTo 0
      End Function
      
    • Hi David Lee,
      Tks for your faster answer. But it has an error in this part:

      For Each olkSub In olkFol.Folders
      ExportMessagesToExcel olkSub
      DoEvents
      Next
      ***Run´time error ‘-16633827710 (9cd40102)’

      Could not complete the operation because the service provider foes not support it.***

      Could you help me? Maybe I’m confused in what put on the new fields “Store” and “folder”.

    • Vagston,

      What type of mail server is your mailbox on? By “type” I mean is it Exchange, Gmail, or something else?

    • Vagston,

      I see the problem. Replace the subroutine ExportMessagesToExcel with the version below. Leave the rest of the code as is.

      Private Sub ExportMessagesToExcel(olkFol As Outlook.MAPIFolder)
          '--> Declare some variables
          Dim olkMsg As Object, _
              olkAtt As Object, _
              objFol As Object, _
              olkLst As Object, _
              strAtt As String
          'Process the current folder and write its messages to the spreadsheet
          Set olkLst = olkFol.Items.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
          'Write messages to spreadsheet
          For Each olkMsg In olkLst
              'Only export messages, not receipts or appointment requests, etc.
              If olkMsg.Class = olMail Then
                  strAtt = ""
                  'Create a new file system folder to store the message and its attachments
                  Set objFol = objFSO.CreateFolder(ROOT_PATH & "\" & olkMsg.EntryID)
                  'Save a plain-text version of the email to the folder
                  olkMsg.SaveAs objFol.Path & "\Message.txt", olTXT
                  'Save the attachments to the folder
                  For Each olkAtt In olkMsg.Attachments
                      If Not IsHiddenAttachment(olkAtt) Then
                          olkAtt.SaveAsFile objFol.Path & "\" & olkAtt.FileName
                          strAtt = strAtt & olkAtt.FileName & ", "
                      End If
                  Next
                  If Len(strAtt) > 0 Then strAtt = Left(strAtt, Len(strAtt) - 2)
                  'Add a row for each field in the message you want to export
                  excWks.Cells(lngRow, 1) = olkMsg.Parent.FolderPath
                  excWks.Cells(lngRow, 2) = olkMsg.Subject
                  excWks.Cells(lngRow, 3) = olkMsg.To
                  excWks.Cells(lngRow, 4) = olkMsg.SenderName
                  excWks.Cells(lngRow, 5) = olkMsg.CC
                  excWks.Cells(lngRow, 6) = strAtt
                  excWks.Cells(lngRow, 7) = olkMsg.EntryID
                  excWks.Hyperlinks.Add Anchor:=excWks.Range("H" & lngRow, "H" & lngRow), Address:="file://" & objFol.Path, TextToDisplay:="View Original Items"
                  lngRow = lngRow + 1
                  lngCnt = lngCnt + 1
                  Set olkAtt = Nothing
              End If
              DoEvents
          Next
          Set olkLst = Nothing
          Set olkMsg = Nothing
          Set objFol = Nothing
      End Sub
      
    • David,
      now the code in run well, but isn’t exporting emails. Could you explain better what I need to put on the fields “store” and “folder”?

      I inserted that: store = my email(location of search folder) and Folder: “All documentos(search folder name)

      Thank you.

    • Vagston,

      The folder name is the name of the search folder. The store is the name of the message store the search folder is in. On an Exchange server mailbox the store name is normally either “Mailbox – Doe, John” or “john.doe@company.com”.

    • Hi David, Only one more question. Is it possible to change the code to classify the spreadsheet by email date(Cells(1,1) = Date)?

      ‘Write Excel Column Headers
      With excWks
      .Cells(1, 1) = “Date”
      .Cells(1, 2) = “Time”
      .Cells(1, 3) = “Subject”
      .Cells(1, 4) = “To”
      .Cells(1, 5) = “From”
      .Cells(1, 6) = “Attachments”
      .Cells(1, 7) = “ID”
      .Cells(1, 8) = “Link”
      .Cells(1, 9) = “CC”
      .Cells(1, 10) = “Body”
      .Cells(1, 11) = “Folder”
      End With

      excWks.Cells(lngRow, 1) = olkMsg.ReceivedTime
      excWks.Cells(lngRow, 2) = Format(olkMsg.ReceivedTime, “hh:nn”)
      excWks.Cells(lngRow, 3) = olkMsg.Subject
      excWks.Cells(lngRow, 4) = olkMsg.To
      excWks.Cells(lngRow, 5) = olkMsg.SenderName
      excWks.Cells(lngRow, 6) = strAtt
      excWks.Cells(lngRow, 7) = olkMsg.EntryID
      excWks.Cells(lngRow, 9) = olkMsg.CC
      excWks.Cells(lngRow, 10) = olkMsg.Body
      excWks.Cells(lngRow, 11) = olkMsg.Parent.FolderPath
      excWks.Hyperlinks.Add Anchor:=excWks.Range(“H” & lngRow, “H” & lngRow), Address:=”file://” & objFol.Path, TextToDisplay:=”View Original Items”

    • means that I need to export the email to spreadsheet by “received time”…Is it possible?

    • Vagston,

      If you mean to sort the exported email by received time, then it would be easier for you to do that in Excel. If you don’t want to do that, then I can add code that will do it. It’s your choice.

    • Hi David, I know that I could to that easily in Excel but I prefer that the code do that every time that I export emails with date range, because the data are smaller than spreadsheet. Thanks.

    • Vagston,

      Ok. Replace the subroutine ExportMessagesToExcel with the version below. Leave the rest of the code as is.

      Private Sub ExportMessagesToExcel(olkFol As Outlook.MAPIFolder)
          '--> Declare some variables
          Dim olkMsg As Object, _
              olkAtt As Object, _
              olkCol As Object, _
              olkLst As Object, _
              objFol As Object, _
              strAtt As String
          'Process the current folder and write its messages to the spreadsheet
          Set olkCol = olkFol.Items
          olkCol.Sort "ReceivedTime"
          Set olkLst = olkCol.Restrict("[ReceivedTime] >= '" & Format(datStart, "ddddd h:nn AMPM") & "'" & " AND [ReceivedTime] <= '" & Format(datEnd, "ddddd h:nn AMPM") & "'")
          'Write messages to spreadsheet
          For Each olkMsg In olkLst
              'Only export messages, not receipts or appointment requests, etc.
              If olkMsg.Class = olMail Then
                  strAtt = ""
                  'Create a new file system folder to store the message and its attachments
                  Set objFol = objFSO.CreateFolder(ROOT_PATH & "\" & olkMsg.EntryID)
                  'Save a plain-text version of the email to the folder
                  olkMsg.SaveAs objFol.Path & "\Message.txt", olTXT
                  'Save the attachments to the folder
                  For Each olkAtt In olkMsg.Attachments
                      If Not IsHiddenAttachment(olkAtt) Then
                          olkAtt.SaveAsFile objFol.Path & "\" & olkAtt.FileName
                          strAtt = strAtt & olkAtt.FileName & ", "
                      End If
                  Next
                  If Len(strAtt) > 0 Then strAtt = Left(strAtt, Len(strAtt) - 2)
                  'Add a row for each field in the message you want to export
                  excWks.Cells(lngRow, 1) = olkMsg.Parent.FolderPath
                  excWks.Cells(lngRow, 2) = olkMsg.Subject
                  excWks.Cells(lngRow, 3) = olkMsg.To
                  excWks.Cells(lngRow, 4) = olkMsg.SenderName
                  excWks.Cells(lngRow, 5) = olkMsg.CC
                  excWks.Cells(lngRow, 6) = strAtt
                  excWks.Cells(lngRow, 7) = olkMsg.EntryID
                  excWks.Hyperlinks.Add Anchor:=excWks.Range("H" & lngRow, "H" & lngRow), Address:="file://" & objFol.Path, TextToDisplay:="View Original Items"
                  lngRow = lngRow + 1
                  lngCnt = lngCnt + 1
                  Set olkAtt = Nothing
              End If
              DoEvents
          Next
          Set olkCol = Nothing
          Set olkLst = Nothing
          Set olkMsg = Nothing
          Set objFol = Nothing
      End Sub
      
    • Chris,

      Here is the code. So long as the message are in one of the four formats you shared with me, this should work. It did work with all four of the sample messages you provided. Please try it out and let me know if it does what you wanted.

      'On the next line edit the path to and name of the Excel spreadhsheet the solution will export to
      Const EXCEL_FILE = "C:\users\david\documents\testarea\XpertNetworking.xlsx"
      Const MACRO_NAME = "Export Messages to Excel (Rev 14)"
      
      Sub ExportMessagesToExcel()
          Dim olkMsg As Object, _
              excApp As Object, _
              excWkb As Object, _
              excWks As Object, _
              lngRow As Long, _
              lngCnt As Long, _
              strOID As String, _
              strKey As String, _
              strAdr As String, _
              strTel As String, _
              strDel As String
          On Error Resume Next
          Set excApp = CreateObject("Excel.Application")
          Set excWkb = excApp.Workbooks.Open(EXCEL_FILE)
          Set excWks = excWkb.Worksheets(Application.ActiveExplorer.CurrentFolder.Name)
          If TypeName(excWks) = "Nothing" Then
              Set excWks = excWkb.Worksheets.Add
              excWks.Name = Application.ActiveExplorer.CurrentFolder.Name
              'Write Excel Column Headers
              With excWks
                  .Cells(1, 1) = "OrderID"
                  .Cells(1, 2) = "Key"
                  .Cells(1, 3) = "Email"
                  .Cells(1, 4) = "Telephone"
                  .Cells(1, 5) = "Address"
              End With
              lngRow = 2
          Else
              lngRow = excWks.UsedRange.rows.Count + 1
          End If
          excApp.Visible = True
          'Write messages to spreadsheet
          For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
              'Only export messages, not receipts or appointment requests, etc.
              If olkMsg.Class = olMail Then
                  strOID = ""
                  strKey = ""
                  strAdr = ""
                  strTel = ""
                  strDel = ""
                  strOID = FindString(olkMsg.Body, "\(OrderID:(.+)\)")
                  If strOID = "Not found" Then
                      strOID = FindString(olkMsg.Body, "ORDER #:(.+)\b")
                      strOID = Replace(strOID, "ORDER #:", "")
                  Else
                      strOID = Replace(strOID, "(OrderID:", "")
                      strOID = Replace(strOID, ")", "")
                  End If
                  strKey = FindString(olkMsg.Body, "Key:\s(.+)\b")
                  If strKey <> "Not found" Then
                      strKey = Replace(strKey, "Key:", "")
                  End If
                  strAdr = FindString(olkMsg.Body, "EMail:\s(.+)\b")
                  If strAdr = "Not found" Then
                      strAdr = FindString(olkMsg.Body, "Email:(.+)\b")
                      strAdr = Replace(strAdr, "Email:", "")
                  Else
                      strAdr = Replace(strAdr, "EMail:", "")
                  End If
                  strTel = FindString(olkMsg.Body, "Numéro de Téléphone:(.+)\b")
                  If strTel = "Not found" Then
                      strTel = ""
                  Else
                      strTel = Replace(strTel, "Numéro de Téléphone:", "")
                  End If
                  strDel = FindString(olkMsg.Body, "DELIVERY ADDRESS:([\s\S]*)Numéro")
                  If strDel = "Not found" Then
                      strDel = ""
                  Else
                      strDel = Replace(strDel, "DELIVERY ADDRESS:", "")
                      strDel = Replace(strDel, "Numéro", "")
                  End If
                  excWks.Cells(lngRow, 1) = Trim(strOID)
                  excWks.Cells(lngRow, 2) = Trim(strKey)
                  excWks.Cells(lngRow, 3) = Trim(strAdr)
                  excWks.Cells(lngRow, 4).NumberFormat = "@"
                  excWks.Cells(lngRow, 4) = Trim(strTel)
                  excWks.Cells(lngRow, 5) = Trim(strDel)
                  lngRow = lngRow + 1
                  lngCnt = lngCnt + 1
              End If
          Next
          excWks.Columns("A:E").AutoFit
          excWkb.Close True
          Set olkMsg = Nothing
          Set excWks = Nothing
          Set excWkb = Nothing
          Set excApp = Nothing
          On Error GoTo 0
          MsgBox "Process complete.  A total of " & lngCnt & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
      End Sub
       
      Private Function FindString(strText As String, strFind As String) As String
          Dim objRegEx As Object, colMatches As Object, objMatch As Object
          Set objRegEx = CreateObject("VBscript.RegExp")
          With objRegEx
              .IgnoreCase = False
              .Global = True
              .Pattern = strFind
              Set colMatches = .Execute(strText)
          End With
          If colMatches.Count > 0 Then
              Set objMatch = colMatches.Item(0)
              FindString = objMatch.Value
          Else
              FindString = "Not found"
          End If
          Set objRegEx = Nothing
          Set colMatches = Nothing
          Set objMatch = Nothing
      End Function
      
  9. Thank you very much David; i almost missed your response there
    the body of the email has few variations to it; but its fairly simple

    v1

    A new order (OrderID: 529264) of ESN - Security token - Request is made. (Key: 69YqtLOptueq7rPwt+GZ57X0t+Wt9beq+ej8pPmk+aQ=)
    EMail: jcotstis@plannet.nl
    

    v2

    Access request Security token
    A new order (OrderID: 529264) of ESN - Security token - Request is made.
    Key: 69YqtLOptueq7rPwt+GZ57X0t+Wt9beq+ej8pPmk+aQ=
    EMail: jcotstis@plannet.nl
    

    v3

    Access request Security token
    A new order (OrderID: 529264) of ESN - Security token - Request is made.
    Key: Order status is not proved!
    EMail: jcotstis@plannet.nl
    

    im only looking to extract ORderID, Key, and Email from the body itself
    maybe a phone number later or another keyword down the road

    my incoming emails are routed to specific folder with rules based on specific sender
    i will run this script essentially standing over the sub folder i wish to query

    the code i posted before only catches the email and places it in the right position
    the other are ignored i think; not sure why
    so frustrating; if only i understood the logic little better
    ive edited some scripts in the past; i can Google my way through most the time
    but this VS stuff is puzzling to me; i appreciate your help greatly

    Thank you in advance

    Chris

    • I couldn’t find my post on your blog; i must be getting old keeping up with today trends
      Anyways attached is an example output from the script (sent to your private email); you can see the additional text in the OrderID field

      What do I need to comment out to deactivate the address output?

      Best regards

      Chris Bell

  10. I’m trying to something similar to revision 8. I’d like to end up with all of the same information including sender, date and time, recipient, subject and of course the table information. Ive used Outlook Spy to see what I’m looking for but still cant figure this out. Care to help? I’m not a programmer.

    The property tag for the table seems to be PR_BODY_W
    Tag num: 0x1000001F
    Tag sym: PR_BODY_W
    Type: PT_UNICODE
    DASL: http://schemas.microsoft.com/mapi/proptag/0x1000001F

    I can share a copy of a message if you’d like to try it.

    • Hi, Rob.

      I’ll help if I can. Send me the message and I’ll have a look. It’s actually best to send me a message with the sample message attached. That way none of the formatting is disturbed.

    • what is a good email address to send it to? I also found another one of your macros that works with one exception.

      Script 29 seems to do what I need with the exception of the constant prompts for the digitally signed messages to be exported in a format that is not secure manner, Can you add an option to answer this only once or remove it completely?

  11. Ok sorry if this is a stupid question but I’m getting a compile error and its high lighting lngCol as variable not defined?

    • Daniel,

      Option Explicit tells the compiler that every variable has to be defined ahead of time (i.e. no dynamic variables). This means that there must be a DIM statement for every variable. The DIMs can all be on one line

      Dim variableA, variableB, variableC
      

      or across multiple lines

      Dim variableA
      Dim variableB
      Dim variableC
      

      or a combination of the two

      Dim variableA
      Dim variableB, variableC
      

      It’s up to you how you do, but you must define every variable before it’s used.

    • I hate to be a pain david but can you email me too, so i could send you my code? I still cant seem to get this right.

  12. i forgot to include my current script

    Const MACRO_NAME = "Export Messages to Excel (Rev 14)"
    
    Sub ExportMessagesToExcel()
        Dim olkMsg As Object, _
            excApp As Object, _
            excWkb As Object, _
            excWks As Object, _
            intRow As Integer, _
            intVersion As Integer, _
            strBuffer As String, _
            strFilename As String, _
            strTemp As String, _
            arrLines As Variant, _
            varLine As Variant, _
            bolComments As Boolean
        strFilename = "C:\Examples\OutlookItems.xlsx"
        If strFilename <> "" Then
            intVersion = GetOutlookVersion()
            Set excApp = CreateObject("Excel.Application")
            Set excWkb = excApp.Workbooks.Add()
            Set excWks = excWkb.ActiveSheet
            'Write Excel Column Headers
            With excWks
                .Cells(1, 1) = "OrderID"
                .Cells(1, 2) = "Email"
                .Cells(1, 3) = "Key"
                
            End With
            intRow = 2
            'Write messages to spreadsheet
            For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
                'Only export messages, not receipts or appointment requests, etc.
                If olkMsg.Class = olMail Then
                    'Add a row for each field in the message you want to export
                    strBuffer = ""
                    bolComments = False
                    arrLines = Split(olkMsg.Body, vbCrLf)
                    For Each varLine In arrLines
                        strTemp = Trim(varLine)
                        If bolComments Then
                            strBuffer = strBuffer & strTemp
                        Else
                                    If Left(strTemp, 5) = "OrderID: " Then
                                    excWks.Cells(intRow, 1) = Mid(strTemp, 6)
                                Else
                                    If Left(strTemp, 7) = "EMail: " Then
                                    excWks.Cells(intRow, 2) = Mid(strTemp, 8)
                                Else
                                    If Left(strTemp, 9) = "Key: " Then
                                    excWks.Cells(intRow, 3) = Mid(strTemp, 10)
                                    bolComments = True
                                Else
                            End If
                        End If
                    End If
                End If
            Next
                excWks.Cells(intRow, 5) = strBuffer
                intRow = intRow + 1
        End If
            Next
            Set olkMsg = Nothing
            excWkb.SaveAs strFilename
            excWkb.Close
        End If
        Set excWks = Nothing
        Set excWkb = Nothing
        Set excApp = Nothing
        MsgBox "Process complete.  A total of " & intRow - 2 & " messages were exported.", vbInformation + vbOKOnly, "Export messages to Excel"
    End Sub
    
    Private Function GetSMTPAddress(Item As Outlook.MailItem, intOutlookVersion As Integer) As String
        Dim olkSnd As Outlook.AddressEntry, olkEnt As Object
        On Error Resume Next
        Select Case intOutlookVersion
            Case Is < 14
                If Item.SenderEmailType = "EX" Then
                    GetSMTPAddress = SMTP2007(Item)
                Else
                    GetSMTPAddress = Item.SenderEmailAddress
                End If
            Case Else
                Set olkSnd = Item.Sender
                If olkSnd.AddressEntryUserType = olExchangeUserAddressEntry Then
                    Set olkEnt = olkSnd.GetExchangeUser
                    GetSMTPAddress = olkEnt.PrimarySmtpAddress
                Else
                    GetSMTPAddress = Item.SenderEmailAddress
                End If
        End Select
        On Error GoTo 0
        Set olkPrp = Nothing
        Set olkSnd = Nothing
        Set olkEnt = Nothing
    End Function
    
    Function GetOutlookVersion() As Integer
        Dim arrVer As Variant
        arrVer = Split(Outlook.Version, ".")
        GetOutlookVersion = arrVer(0)
    End Function
    
    Function SMTP2007(olkMsg As Outlook.MailItem) As String
        Dim olkPA As Outlook.PropertyAccessor
        On Error Resume Next
        Set olkPA = olkMsg.PropertyAccessor
        SMTP2007 = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001E")
        On Error GoTo 0
        Set olkPA = Nothing
    End Function
    
    
    
    • Hi David, ive tested the script which works great
      Just as I wanted it to work; I can’t thank you enough
      If I can be of any help to you down the road; please don’t hesitate to ask

      One question; in the ordered field
      Is it possible to truncate the text that comes after it so the line only contains the order number?

      Thanks again for your diligent work

      Best regards

      Chris Bell

    • Chris,

      You’re welcome!

      Sorry but I’m not clear on what you mean when you say “truncate the text”. Truncate it how? What is it that you see after the order number that shouldn’t be there?

  13. David,
    You are amazing. With your help I’ve been finally able to move past the wall i was hitting in my vba code. I have used your main code and it works beautifully but, I was wondering if you could help me with parsing the data in the body of the emails i will be using. Here’s what the email body looks like:
    197392-1
    197525-2
    140459-3
    140467-4
    170092-1
    274498-3
    201681-2
    125161-1
    195818-1
    195834-1
    196907-2
    196998-1
    155028-9
    How can i make it do this each being in its separate row and column.
    197392 1
    197525 2
    140459 3
    140467 4
    170092 1
    274498 3
    201681 2
    125161 1
    195818 1
    195834 1
    196907 2
    196998 1
    155028 9
    Sorry for the long winded question, and thank you for all of your knowledge and help.

  14. Hello David
    Im very happy I found your thread; excellent walkthroughs; I’m very impressed
    Ive been trying to mix and match parts of your code with not much success
    I’m trying to achieve something fairly simple; but im getting lost and I could use an expert help

    I have a folder with email I route through the day
    The email is very simple

    A new order (OrderID: 123679) of asn – Environment – is made. (Key: 67pioyrUYF3lopivfGv67rtuq8901yyt029,ud0ejuOm17bqqtOs=)
    EMail: lud.delop@gmail.com

    I just need to extract the OrderID, Email, and key (no need for any other data)
    From multiple items in a single folder

    I have several folders I intend to run this against, one by one manually
    If the macro could be made to append the output from each folder name to a sheet name;
    than scanning nested folder is a very desirable behavior instead of manually running this against each folder

    thank you for your assistance

    kind regards

    Chris Bell

    • Hi, Chris.

      Can you share a sample message with me? It’s hard for me to know how to extract text from the body without knowing exactly how the body is laid out. If you can share a sample message with me, then please email me the message. Copy and paste doesn’t help as it doesn’t allow me to see the underlying layout.

    • Daniel,

      Ok, that makes it easy. The simplest solution is to turn the body into an array of lines. Something like this

      arrLines = Split(olkMsg.Body, vbCrLf)
      

      That line of code splits the body into lines based on the carriage return/linefeed character pair that occurs at the end of each line. You can then iterate through the lines with something like this

      'Set a variable to the starting column number.  Replace x with some value.
      lngCol = x
      For Each varLine in arrLines
          excWks.Cells(lngRow, lngCol) = varLine
          lngCol = lngCol + 1
      Next
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s