I have a project that needs both Outlook and Excel connections. What I need is:
Extract data of Unread emails from the Outlook Inbox (date received, sender, subject) then paste in Excel. See code below.
After data is pasted in Excel, format the extracted data (I already have the code in Excel) then save the Excel file.
Mark the emails that were extracted as "Read"
My Questions are:
Is there a way to run the code automatically (ex: run at 7am, 2pm and 6am)?
How will I set the emails as "read" after the information has been extracted?
How will I select only "Unread" emails to be extracted?
How can I connect my Outlook code to my Excel code and run it automatically?
Code in Outlook to extract data:
Sub Extract()
Set myOlApp = Outlook.Application
Set mynamespace = myOlApp.GetNamespace("mapi")
Set myfolder = myOlApp.ActiveExplorer.CurrentFolder
Set xlobj = CreateObject("Excel.Application")
xlobj.Visible = True
xlobj.workbooks.Add
'Set Heading
xlobj.Range("a" & 1).Value = "Received time"
xlobj.Range("b" & 1).Value = "Subject"
xlobj.Range("c" & 1).Value = "Importance"
xlobj.Range("d" & 1).Value = "Sender"
For i = 1 To myfolder.Items.Count
Set myitem = myfolder.Items(i)
xlobj.Range("a" & i + 1).Value = myitem.ReceivedTime
xlobj.Range("b" & i + 1).Value = myitem.Subject
xlobj.Range("c" & i + 1).Value = myitem.Importance
xlobj.Range("d" & i + 1).Value = myitem.SenderName
Next
End Sub