通过迭代保存从电子邮件中提取的附件 [英] Save attachments extracted from e-mail by iterating

查看:31
本文介绍了通过迭代保存从电子邮件中提取的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够识别并提取我需要的所有必要电子邮件.我还将每个电子邮件的附件保存到另一个变量.但是,我在将这些附件保存到本地文件夹时遇到了问题,特别是那些文件类型= .xlsx的附件.

I was able to identify and extract all necessary e-mails I need. I also saved the attachments per e-mail to another variable. However, I'm having issue saving these attachments to a local folder, specifically those that are of file type = .xlsx.

library(RDCOMClient)
setwd("C:/Updated")
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject like '%Daily Efficiency Tracker%'"
)

Sys.sleep(10)
results <- search$Results()

attachment_file <- getwd()

for (i in 1:results$Count()) {
  if (as.Date("1899-12-30") + floor(results$Item(i)$ReceivedTime()) 
      == as.Date("2019-04-02")) {
    email <- results$Item(i)
    attachment <- email$Attachments()
    for(j in 1:attachment$Count()){
      if (grepl(".xlsx", attachment$Item(i)$FileName(), ignore.case = TRUE)) {
        attachment$Item(i)$SaveAsFile(attachment_file)
      }
    }
  }
}

当我逐行运行它时,我在这部分上只有错误:

When I run it line by line, I only had error on this part:

attachment$Item(i)$SaveAsFile(attachment_file)

下面是错误消息:

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

推荐答案

我已经设法执行以下操作,但是,如果您收到一条消息,但没有要查找的指定文件附件,则会导致错误.

I've managed to do the below however, it results to an error if you receive a message WITHOUT the specified file attachment you are looking for.

library(RDCOMClient)
library(openxlsx)

setwd("C:/Users/JGGliban/Desktop/Work/ADMIN/Data Integrity/DI OT Tracker")
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject like '%Daily Efficiency and OT Tracker%'"
)

Sys.sleep(10)
results <- search$Results()
attachment_file <- tempfile()

date <- function(){
  if ((wday(format(Sys.Date(), "%Y-%m-%d"), label = FALSE)) == 1){
    return(format(Sys.Date()-3, "%Y-%m-%d"))
  } else {
    return(format(Sys.Date()-1, "%Y-%m-%d"))
  }
}

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

    for(j in 1:attachment$Count()) {
      if (grepl(".xlsx", attachment$Item(j)$FileName(), ignore.case = TRUE)) {
        attachmentname <- attachment$Item(j)$FileName()
        attachment_file <- paste0(getwd(), "/", attachmentname)
        attachment$Item(j)$SaveAsFile(attachment_file)
      }
      Sys.sleep(10)
    }
  }
}

这篇关于通过迭代保存从电子邮件中提取的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆