Sub ExportToExcel()
Dim msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim appExcel As Object
Dim sRow As Integer
Set appExcel = GetObject(, "Excel.Application")
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder
If fld Is Nothing Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
ElseIf fld.DefaultItemType <> olMailItem Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
ElseIf fld.Items.Count = 0 Then
MsgBox "There are no mail messages to export", vbOKOnly, "Error"
Exit Sub
End If
appExcel.Workbooks.Add
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.Sheets(1)
wks.Activate
appExcel.Application.Visible = True
appExcel.Application.DisplayAlerts = False
'On Error Resume Next
For Each itm In fld.Items
sRow = sRow + 1
appExcel.Range("A" & sRow).Value = itm.SentOn
appExcel.Range("B" & sRow).Value = itm.SenderEmailAddress
appExcel.Range("C" & sRow).Value = itm.Subject
appExcel.Range("D" & sRow).Value = itm.Body
'appExcel.Range("D" & sRow).Replace What:="" & Chr(10) & "", Replacement:=" ", LookAt:=xlPart, _
'SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
'ReplaceFormat:=False
'appExcel.Range("D" & sRow).Replace What:="" & Chr(13) & "", Replacement:=" ", LookAt:=xlPart, _
'SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
'ReplaceFormat:=False
appExcel.Range("F" & sRow).Value = itm.SentOn
appExcel.Range("G" & sRow).Value = itm.ReceivedTime
Next itm
appExcel.Application.DisplayAlerts = True
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
End Sub