I want to export Outlook email body text to an Excel spreadsheet.
I get "Range' of object'_global' failed" on
If OutlookMail.ReceivedTime >= Range("Email_Receipt_Date").Value Then
Before running the code, I closed Outlook, I checked the categories in the Excel spreadsheet, and I selected all the correct references. From my understanding, it's not recognizing the date.
Each named range refers to a single cell in Excel.
Sub getDataFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("staffingMail")
i = 1
For Each OutlookMail In Folder.Items
If OutlookMail.ReceivedTime >= Range("Email_Receipt_Date").Value Then
Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
Range("eMail_subject").Offset(i, 0).Columns.AutoFit
Range("eMail_subject").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
Range("eMail_date").Offset(i, 0).Columns.AutoFit
Range("eMail_date").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
Range("eMail_sender").Offset(i, 0).Columns.AutoFit
Range("eMail_sender").Offset(i, 0).VerticalAlignment = xlTop
Range("eMail_Body").Offset(i, 0).Value = OutlookMail.Body
Range("eMail_Body").Offset(i, 0).Columns.AutoFit
Range("eMail_Body").Offset(i, 0).VerticalAlignment = xlTop
i = i + 1
End If
Next OutlookMail
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub