I have a macro in Excel to go into a specific Outlook folder. For all emails in that folder I want to copy the sender email address, subject, body, and date into Excel. When I run the code it returns a an error 'Run-time error 438: Object doesnt support this property or method'. Can someone please advise as to why I am getting this error?
The lines that are not working are in red font below:
Option Explicit
Sub EmailText()
Dim objOutlook As Object
Dim Mynamespace As Object
Dim i As Integer
Dim oRow As Integer
Dim abody() As String
Set objOutlook = GetObject(, "Outlook.Application")
Set Mynamespace = objOutlook.GetNamespace("MAPI")
ThisWorkbook.Sheets(1).Activate
ThisWorkbook.Sheets(1).Cells(1, 1) = "Sender"
ThisWorkbook.Sheets(1).Cells(1, 2) = "Date"
ThisWorkbook.Sheets(1).Cells(1, 3) = "Subject"
ThisWorkbook.Sheets(1).Cells(1, 4) = "Body"
oRow = 1
For i = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Count To 1 Step -1
oRow = oRow + 1
ThisWorkbook.Sheets(1).Cells(oRow, 1).Select
ThisWorkbook.Sheets(1).Cells(oRow, 1) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).SenderEmailAddress
ThisWorkbook.Sheets(1).Cells(oRow, 2) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).ReceivedTime
ThisWorkbook.Sheets(1).Cells(oRow, 3) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).Subject
ThisWorkbook.Sheets(1).Cells(oRow, 4) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).Body
Next i
Set objOutlook = Nothing
Set Mynamespace = Nothing
End Sub
Thanks!
The lines that are not working are in red font below:
Option Explicit
Sub EmailText()
Dim objOutlook As Object
Dim Mynamespace As Object
Dim i As Integer
Dim oRow As Integer
Dim abody() As String
Set objOutlook = GetObject(, "Outlook.Application")
Set Mynamespace = objOutlook.GetNamespace("MAPI")
ThisWorkbook.Sheets(1).Activate
ThisWorkbook.Sheets(1).Cells(1, 1) = "Sender"
ThisWorkbook.Sheets(1).Cells(1, 2) = "Date"
ThisWorkbook.Sheets(1).Cells(1, 3) = "Subject"
ThisWorkbook.Sheets(1).Cells(1, 4) = "Body"
oRow = 1
For i = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Count To 1 Step -1
oRow = oRow + 1
ThisWorkbook.Sheets(1).Cells(oRow, 1).Select
ThisWorkbook.Sheets(1).Cells(oRow, 1) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).SenderEmailAddress
ThisWorkbook.Sheets(1).Cells(oRow, 2) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).ReceivedTime
ThisWorkbook.Sheets(1).Cells(oRow, 3) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).Subject
ThisWorkbook.Sheets(1).Cells(oRow, 4) = Mynamespace.GetDefaultFolder(olFolderInbox).Folders("Test").Items.Item(i).Body
Next i
Set objOutlook = Nothing
Set Mynamespace = Nothing
End Sub
Thanks!