3

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!

nak5120
  • 4,089
  • 4
  • 35
  • 94
  • Did you check that there are attachments? Check if Attachments.Count > 0. Also make sure that attachment_file points to a fully qualified fiel name including the path. – Dmitry Streblechenko Dec 17 '17 at 16:34
  • Yup just checked and there is an attachment within the zip folder in the screenshot – nak5120 Dec 17 '17 at 16:35
  • @DmitryStreblechenko actually is that a command in R that I can check? `Attachments.Count > 0`? If so, where would I place that in the script above? – nak5120 Dec 17 '17 at 16:38
  • I use the exact same script, we probably both got it from the same SO answer. I haven't used mine in ages, and its actually giving the same error. Its possible someone is developing in one of the packages? – Matt W. Dec 17 '17 at 16:40
  • The weird thing is that it works for a normal csv file, just not a zipped file. – nak5120 Dec 17 '17 at 16:42
  • I think I figured it out on my end. But try my answer and lmk what you find. – Matt W. Dec 17 '17 at 16:49
  • How could I run this search against an Outlook Subfolder? – Matt Gossett Dec 03 '20 at 13:06

1 Answers1

2

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)
  }
}
Matt W.
  • 3,692
  • 2
  • 23
  • 46
  • I think that actually might have worked. The only error I am having now is this: Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input In addition: Warning messages: 1: In readLines(file, skip) : line 2 appears to contain an embedded nul 2: In readLines(file, skip) : incomplete final line found on 'C:\Users\nickk\AppData\Local\Temp\RtmpWOPe51\file20943524103f' – nak5120 Dec 17 '17 at 16:52
  • Do you think that just has to do with the zip file? – nak5120 Dec 17 '17 at 16:53
  • Inside the zip file is a csv file with the same file name – nak5120 Dec 17 '17 at 16:54
  • the resulting dataframe read in R is a 1x2 dataset of weird characters even though the actual dataset in the attachment has thousands of rows and about 40-50 columns – nak5120 Dec 17 '17 at 16:57
  • not 100% sure, I would need a reproducible example. print out your `attachment_file` to the console and follow it. Then try to open it somewhere and see what it is. For mine, it's a csv. so read.csv works. I think if yours is a zip that you're downloading, you'll need to use a zip package to open it up – Matt W. Dec 17 '17 at 17:03
  • pull your normal attachment into your working directory, and then try to bring in what you're looking for just using like a 7-zip package in R. Then replace the `read.csv` portion of this code with whatever you use that successfully unzips the folder and pulls out the csv from that. – Matt W. Dec 17 '17 at 17:05
  • W I know this has already been accepted. But any other advice on how to read the zip file, still having trouble with that – nak5120 Dec 17 '17 at 19:45
  • check out the top answer on this Q: [using r to download zipped data file](https://stackoverflow.com/questions/3053833/using-r-to-download-zipped-data-file-extract-and-import-data) – Matt W. Dec 17 '17 at 19:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161419/discussion-between-nick-knauer-and-matt-w). – nak5120 Dec 18 '17 at 04:32
  • Have you ever experienced a situation like this: https://stackoverflow.com/questions/48695427/extracting-zipcsv-file-from-attachment-w-image-in-body-of-email – nak5120 Feb 09 '18 at 16:07