Nick Knauer
Nick Knauer

Reputation: 4243

Reading Email Attachment to R

I am using R to read an Outlook attachment. My reference is here: Download attachment from an outlook email using R

This is a screenshot of what my email looks like:

enter image description here

This gets sent to me daily.

When I try extracting this attachment this is how I went about it:

install.packages('RDCOMClient')
library(RDCOMClient)
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject = 'DDM Report #107047216 : \"Nick_ACS_Brand_All_FloodLights\" from Nicholas Knauer'"
)


results <- search$Results()
results$Item(1)$ReceivedTime() # Received time of first search result
as.Date("1899-12-30") + floor(results$Item(1)$ReceivedTime()) # Received date

for (i in 1:results$Count()) {
  if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) 
      == as.Date("2017-12-17")) {
    email <- results$Item(i)
  }
}

attachment_file <- tempfile()
email$Attachments(1)$SaveAsFile(attachment_file)
data <- read.csv(attachment_file, skip = 10)

After I run results$Item(1)$ReceivedTime(), this error comes up:

<checkErrorInfo> 80020009 
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Error: Exception occurred.

Any idea how I can fix this issue?

Thanks!

Upvotes: 3

Views: 4636

Answers (1)

Matt W.
Matt W.

Reputation: 3722

Try putting Sys.sleep(5) (if that doesn't work, try Sys.sleep(10))in between saving as results, and results$Item(1)$ReceivedTime(). I think it needs time to process in between.

results <- search$Results() # Saves search results into results object

Sys.sleep(5) # Wait a hot sec!

results$Item(1)$ReceivedTime() # Received time of first search result

as.Date("1899-12-30") + floor(results$Item(1)$ReceivedTime()) # Received date

# Iterates through results object to pull out all of the items
for (i in 1:results$Count()) {
  if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) 
      == as.Date(Sys.Date())) {
    email <- results$Item(i)
  }
}

Upvotes: 2

Related Questions