Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

How to retrieve Outlook inbox emails using R RDCOMClient?

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

I want to retrieve today's email from an Outlook folder named 'AUX'. Parse through the email's title and if it meets certain conditions, I want to parse the content of the email for certain strings.

I managed to write an email from R and send it out but so far not able to retrieve the emails.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is some sample code I got to work by 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

# Just doing first 10, get total number with emails()$Count()
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 object require parameters (like Subject(1)), but others don't (like Body()). This worked for me on Outlook 2013, but it should also work on all versions of Outlook from 2007 on.

To get more info on the Outlook Object model I would suggest you either get Ken Slovak's Outlook 2007 book (still relevent for later versions of Outlook), or else check out my personal website, http://www.gregthatcher.com (check out the "Scripts" section -- I have been compiling these for many years.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...