【问题标题】:Save attachments extracted from e-mail by iterating通过迭代保存从电子邮件中提取的附件
【发布时间】:2019-08-24 11:22:27
【问题描述】:

我能够识别并提取我需要的所有必要电子邮件。我还将每封电子邮件的附件保存到另一个变量中。但是,我在将这些附件保存到本地文件夹时遇到问题,特别是文件类型 = .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)
      }
    }
  }
}

当我逐行运行时,我只在这部分出错:

attachment$Item(i)$SaveAsFile(attachment_file)

以下是错误信息:

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

【问题讨论】:

    标签: r loops email outlook


    【解决方案1】:

    Windows 在 R 中使用“反斜杠”而不是“正斜杠”。因此,当您提供要保存附件的文件路径时,您需要将“正斜杠”更改为“双反斜杠”。

    使用 gsub 的示例:

    attachment_file<-paste0(gsub("/","**\\\\\\\\**",getwd()), "\\\\", attachmentname)
    

    【讨论】:

      【解决方案2】:

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

      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)
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-11-07
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多