Working on an Excel Macro to go into a specified folder in Outlook and based on a variable (value/named range in Excel) extract data from emails (To, Subject, etc.).
I cannot get it to extract anything besides the "Subject" and "Size" data of the emails.
If I try to pull in the "To" data for example using the same method as the "Subject" or "Size" coding, then it comes up with
"Run-time error '438': Object doesn't support this property or method error.
Sub FetchEmailData()
Dim appOutlook As Object
Dim olNs As Object
Dim olFolder As Object
Dim olItem As Object
Dim iRow As Integer
'Get/create Outlook Application
On Error Resume Next
Set appOutlook = GetObject(, "Outlook.Application")
If appOutlook Is Nothing Then
Set appOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olNs = appOutlook.GetNamespace("MAPI")
Set olFolder = olNs.Folders("Mailbox_name").Folders("Inbox").Folders("XYZ").Folders("2017").Folders("04. April").Folders("Etc")
'Clear
ThisWorkbook.Sheets("Test").Cells.Delete
'Build headings:
ThisWorkbook.Sheets("Test").Range("A1:D1") = Array("Sender_Email_Address", "Subject", "To", "Size")
For iRow = 1 To olFolder.Items.Count
ThisWorkbook.Sheets("Test").Cells(iRow, 1).Select
'ThisWorkbook.Sheets("Test").Cells(iRow, 1) = olFolder.Items.Item(iRow).SenderEmailAddress
ThisWorkbook.Sheets("Test").Cells(iRow, 2) = olFolder.Items.Item(iRow).Subject
'ThisWorkbook.Sheets("Test").Cells(iRow, 3) = olFolder.Items.Item(iRow).To
ThisWorkbook.Sheets("Test").Cells(iRow, 4) = olFolder.Items.Item(iRow).Size
Next iRow
End Sub
How could I extract email fields such as "From" and "To"?
Also, if my Set olFolder
value is a named range in Excel that dynamically changes with the date (=Today()
) and uses Folder_Location
as the named range in Excel, would it be correct to write;
Set olFolder = ThisWorkbook.Sheets("Setup").Range("Folder_Location")
Where
Folder_Location = olNs.Folders("Mailbox_name").Folders("Inbox").Folders("XYZ").Folders("2017").Folders("04. April").Folders("Etc")
This keeps erroring when I attempt to link it to olFolder
.