Задача сценария
Сохранение файлов вложений (.xlsx) из двух электронных писем в общую сетевую папку.
Проблема
R Сценарий ниже отлично работает хорошо в RStudio, но не работает при запуске из Windows командной строки.
Сообщение об ошибке
80020009
Нет поддержки для InterfaceSupportsErrorInfo
checkErrorInfo -2147352567
Ошибка: произошло исключение.
Выполнение остановлено
R Код
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 = "me@me.com;"){
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["sentonbehalfofname"]] = "me@me.com"
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 = "me@me.com",
EmailSubject = paste("[Action Required] Daily Attachment Save Error", Sys.Date()),
EmailBody = paste("Helpful text 1 for troubleshooting"),
AttachFile = F, CC = "")
} else {
SendMail(outFile = "",
ToEmail = "users@many.com",
EmailSubject = paste("Daily attachment saved", Sys.Date()),
EmailBody = paste(msg1),
AttachFile = F, CC = "")
}
if(nchar(msg2)==0){
SendMail(outFile = "",
SendMail(outFile = "",
ToEmail = "me@me.com",
EmailSubject = paste("[Action Required] Daily Attachment Save Error", Sys.Date()),
EmailBody = paste("Helpful text 2 for troubleshooting"),
AttachFile = F, CC = "")
} else {
SendMail(outFile = "",
ToEmail = "users@many.com",
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 Команда выполнения сценария
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% дата: ~ -4,4 %% дата: ~ -7,2 %% дата: ~ -10 , 2% % время: ~ 0,2 %% время: ~ 3,2 %% время: ~ 6,2% .log "2>" C: \ LOGS \ E% дата: ~ -4 , 4 %% дата: ~ -7,2 %% дата: ~ -10,2% * * время тысяча тридцать-семь%: ~ 0,2 %% время: ~ 3,2 %% время: ~ 6,2% .log "
Информация о версии
Windows 10 Pro 64 бит (i7 vPro с 16 ГБ RA M)
R версия 3.4.4 (2018-03-15) - «Кто-то, чтобы опираться»
Microsoft Outlook 2013 (15.0.5172.1000) MSO (15.0.5172.1000) 32-разрядная
RStudio 1.0 .143
Ask
Кто-нибудь сталкивался с этой проблемой с библиотекой RDCOMClient в командной строке? Есть ли исправление? Спасибо.