12
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.

Afiq Johari
  • 1,372
  • 1
  • 15
  • 28

2 Answers2

13

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.)

Mark Neal
  • 996
  • 16
  • 52
Greg Thatcher
  • 1,303
  • 20
  • 29
  • Hi Greg, $folderName doesn't seem to be a proper syntax for R, I omit the $ sign but you use it again for the 'folder' assignment. Is there a reason for this? – Afiq Johari Mar 06 '17 at 08:10
  • The "$" was a mistake. I have edited the answer. Also, make sure you are not running RStudio as Administrator. You should run RStudio under your normal account. – Greg Thatcher Mar 06 '17 at 19:17
  • Can you explain more where do you find the documentation that let you know what COM objects are available to extract? – Afiq Johari Mar 07 '17 at 07:13
  • I have updated my answer with a couple of links. Note that it is often easiest to find some VBA code that does what you want, and then translate this into COM calls. – Greg Thatcher Mar 07 '17 at 19:03
  • try `emails()$Count()` to get the number of emails – shaojl7 Aug 01 '17 at 09:29
  • Has anyone tried this out yet? There is an image in the body of the email that is causing an error in reading the zip file: https://stackoverflow.com/questions/48695427/extracting-zipcsv-file-from-attachment-w-image-in-body-of-email – nak5120 Feb 09 '18 at 14:12
  • hey Greg, would you be able to answer this related question as well? https://stackoverflow.com/questions/52649215/r-rdcomclient-and-outlook-access-inbox-messages-with-shared-addresses – gaut Oct 04 '18 at 15:08
  • 1
    @gpier, I don't have an answer for you, but I would suggest you either try Ken Slovak's book https://www.amazon.com/Professional-Outlook-2007-Programming-Slovak/dp/0470049944 (yes, its old, but still very relevant), or contact Ken Slovak directly at http://www.slovaktech.com/ – Greg Thatcher Oct 04 '18 at 17:12
  • 1
    You can get count like emails()$Count() – Indranil Gayen Nov 26 '19 at 06:01
  • do you have to link to your account in R before you can retrieve emails? are there more examples of using this library for outlook email? – AyeTown Jan 16 '20 at 18:49
  • @GregThatcher do you any idea how do we fetch the Senders Email Address ? The Way I tired doing this like ``` emails(1)[['SenderEmailAddress']] but this gives me values like this : : [1] "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=E4CD239AB9F44AC4AC0A4015B6F4805A-RATINGSDIRE" – Parth Kalra Feb 14 '20 at 06:16
  • 1
    @ParthKalra, that looks like an "Exchange" email address. Here is some VBA code I wrote a long time ago that gets the SMTP address from the Exchange address: http://www.gregthatcher.com/Scripts/VBA/Outlook/GetSmtpAddress.aspx You might try to convert this code into R (that's how I got the above code snippet). – Greg Thatcher Feb 16 '20 at 17:25
3
folderName = "foldername"

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

fld <- outlookNameSpace$GetDefaultFolder(6)

# Check that we got the right folder
fld = fld$folders(folderName)

Cnt = fld$Items()$Count()

emails <- fld$items

df = data.frame(sno = 1:Cnt,Text = "",stringsAsFactors=FALSE)

for(i in seq(Cnt)){
  d = as.data.frame(emails(i)$Body(), stringsAsFactors=FALSE)
  df$Text[i] = d[1]
  df$Sender[i] = emails(i)[['SenderName']]
  df$To[i] = emails(i)[['To']]
  df$sub[i] = emails(i)[['subject']]
}