How to get Outlook Inbox email using RDCOMClient?

library(RDCOMClient)
## create outlook object
OutApp <- COMCreate("Outlook.Application")

      

I want to receive email today from Outlook folder named "AUX". Parse the email title and if it meets certain conditions, I want to parse the message content for specific strings.

I was able to write an email from R and send it, but have not been able to receive emails yet.

0


source to share


1 answer


Here's some sample code I got to work through trial and error:

library(RDCOMClient)

folderName = "AUX"

## create outlook object
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

folder <- outlookNameSpace$Folders(1)$Folders(folderName)
# Check that we got the right folder
folder$Name(1)

emails <- folder$Items

# Can't figure out how to get number of items, so just doing first 10
for (i in 1:10)
{
  subject <- emails(i)$Subject(1)
  # Replace "#78" with the text you are looking for in Email Subject line
  if (grepl("#78", subject)[1]){
    print(emails(i)$Body())
    break
  } 
}

      



Sorry, but I don't know why some of these COM objects require parameters (eg Subject (1)), but others do not (eg Body ()). This worked for me in Outlook 2013, but it should also work in all versions of Outlook since 2007.

For more information on the Outlook object model, I suggest you either get the Ken Slovak Outlook 2007 book (still relevant for later versions of Outlook) or check out my personal site http://www.gregthatcher.com (check out "Scripts" - I've been compiling them for many years.)

+1


source







All Articles