Reputation:
Script Objective
Save attachment files (.xlsx) from two emails to a network share.
Issue
R Script below works perfectly well in RStudio but fails when run from Windows command prompt.
Error Message
< checkErrorInfo> 80020009
No support for InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Error: Exception occurred.
Execution halted
R Code
cat("\n##########################################################################################",
"\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Starting Daily Attachment Save Script...",
"\n##########################################################################################")
cat("\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Setting variables/constants/functions... ")
library("RDCOMClient")
library("lubridate")
TodaysDate <- Sys.Date()
StartTime <- Sys.time()
currentUser <- Sys.getenv("USERNAME")
tmpDir <- Sys.getenv("TEMP")
report_run_date_main <- Sys.Date()
SendMail <- function(outFile,
ToEmail,
EmailSubject,
EmailBody,
AttachFile = TRUE,
CC = "[email protected];"){
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["sentonbehalfofname"]] = "[email protected]"
outMail[["To"]] = ToEmail
outMail[["CC"]] = CC
outMail[["subject"]] = EmailSubject
outMail[["HTMLBody"]] = paste0("<p>**** SYSTEM GENERATED EMAIL ****</p><br><p>", EmailBody, "</p>", sep = "")
if (AttachFile == TRUE) {
outMail[["Attachments"]]$Add(outFile)
}
outMail$Send()
outMail <- NULL
OutApp <- NULL
}
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
"Inbox", paste0("(urn:schemas:httpmail:subject like 'EMAIL SUBJECT TO SEARCH%')")
)
results <- search$Results()
save_folder <- paste0(Sys.getenv("USERPROFILE"), "\\Documents")
get_attachment_names <- function(email) {
number_attachments = email$Attachments()$Count()
if (number_attachments == 0) {
return("")
}
attachments <- purrr::map(
seq(number_attachments),
function(x) email$Attachments(x)$FileName()
)
return(paste(attachments, sep = ", "))
}
msg1 <- ""
msg2 <- ""
cat("done.")
cat("\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Saving attachemnts from daily emails... ")
for (i in 1:results$Count()) {
# Error on next line, when run using command line
gmt_date <- results$Item(i)$ReceivedTime()
date_received <- as.POSIXct(gmt_date * (24 * 60 * 60), origin="1899-12-30", tz="GMT")
date_received_dt <- format(date_received, "%Y-%m-%d")
date_received_tm <- format(date_received, "%H:%M:%S %P")
date_received_hr <- format(date_received, "%H")
if(date_received >= Sys.Date()-1) {
if(date_received_dt==Sys.Date()-1){
if(date_received_hr>=16){
email <- results$Item(i)
attachment_file <- get_attachment_names(email)
email$Attachments(1)$SaveAsFile(paste0(save_folder, attachment_file))
msg1 <- paste(format(Sys.time(), "%a %m/%d/%Y %X"),
"<br><br><b>Attachment name:</b>", attachment_file ,
"<br><b>Email received on:</b>", date_received,
"<br><b>Subject:</b>", email$Subject(),
"<br><b>Save folder:</b>", save_folder)
}
} else{
email <- results$Item(i)
attachment_file <- get_attachment_names(email)
email$Attachments(1)$SaveAsFile(paste0(save_folder, attachment_file))
msg2 <- paste(format(Sys.time(), "%a %m/%d/%Y %X"),
"<br><br><b>Attachment name:</b>", attachment_file ,
"<br><b>Email received on:</b>", date_received,
"<br><b>Subject:</b>", email$Subject(),
"<br><b>Save folder:</b>", save_folder)
}
}
}
cat("done.")
cat("\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Notifying users of attachment save status... ")
if((nchar(msg1)==0)){
SendMail(outFile = "",
ToEmail = "[email protected]",
EmailSubject = paste("[Action Required] Daily Attachment Save Error", Sys.Date()),
EmailBody = paste("Helpful text 1 for troubleshooting"),
AttachFile = F, CC = "")
} else {
SendMail(outFile = "",
ToEmail = "[email protected]",
EmailSubject = paste("Daily attachment saved", Sys.Date()),
EmailBody = paste(msg1),
AttachFile = F, CC = "")
}
if(nchar(msg2)==0){
SendMail(outFile = "",
SendMail(outFile = "",
ToEmail = "[email protected]",
EmailSubject = paste("[Action Required] Daily Attachment Save Error", Sys.Date()),
EmailBody = paste("Helpful text 2 for troubleshooting"),
AttachFile = F, CC = "")
} else {
SendMail(outFile = "",
ToEmail = "[email protected]",
EmailSubject = paste("Daily attachment saved", Sys.Date()),
EmailBody = paste(msg2),
AttachFile = F, CC = "")
}
cat("done.")
cat("\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Garbage Collection... ")
rm(list=ls())
cat("done.")
cat("\n########################################################################################",
"\n", format(Sys.time(), "%a %m/%d/%Y %X"), ": Daily Attachment Save Script complete.",
"\n########################################################################################")
R Script Execution Command
C:\Progra~1\R\R-3.4.4\bin\i386\Rscript.exe --no-save --no-restore --verbose C:\Attachments_Save_Script.R > "C:\LOGS\L%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~0,2%%time:~3,2%%time:~6,2%.log" 2> "C:\LOGS\E%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~0,2%%time:~3,2%%time:~6,2%.log"
Version Info
Windows 10 Pro 64 bit (i7 vPro with 16 GB RAM)
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Microsoft Outlook 2013 (15.0.5172.1000) MSO (15.0.5172.1000) 32-bit
RStudio 1.0.143
Ask
Has anyone come across this issue with RDCOMClient library at the command line? Is there a fix? Thank you.
Upvotes: 0
Views: 265
Reputation: 66286
Your code never checks if you actually have a MailItem
object - you can also have ReportItem
and MeetingItem
objects, which do not expose the ReceivedTime
property. Check if the Class
property (exposed by all OOM objects) == 43
(OlObjectClass.olMail
)
Upvotes: 0