首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用RDCOMClient搜索Outlook收件箱

使用RDCOMClient搜索Outlook收件箱
EN

Stack Overflow用户
提问于 2019-01-30 23:32:05
回答 1查看 1K关注 0票数 2

我正在尝试使用RDCOMClient在我的Outlook收件箱中搜索电子邮件中的特定主题,然后抓取附件。我在一封电子邮件中使用了这个方法,但由于主题包含一个日期元素,所以我需要搜索like子句,但不太清楚这是否适合我下面的查询。

代码语言:javascript
复制
outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  "urn:schemas:httpmail:subject = 'test email executed at 13/01/2019 10:00:08'"
)

我只需要搜索主题行的第一部分,查找日期和时间之前的所有内容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-31 03:19:00

我想像这样的东西应该行得通。它应该搜索包含指定短语的任何邮件,并下载它们的每个附件。

代码语言:javascript
复制
library(RDCOMClient)
library(fs)

search.phrase <- 'test email executed at'

save.fldr <- tempdir() # Set a root folder to save attachments into
print(save.fldr)

outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
  "Inbox",
  paste0("http://schemas.microsoft.com/mapi/proptag/0x0037001E ci_phrasematch '", search.phrase, "'")
)

Sys.sleep(10) # Wait some time to allow search to complete

results <- search[['Results']]

for(i in c(1:results[['Count']])){ # Loop through search results
  attachments.obj <- results[[i]][['attachments']] # Gets the attachment object

  if(attachments.obj[['Count']] > 0){ # Check if there are attachments
    attach.fldr <- file.path(save.fldr, path_sanitize(results[[i]][['Subject']])) # Set folder name for attachments based on email subject

    if(!dir.exists(attach.fldr)){
      dir.create(attach.fldr) # Create the folder for the attachments if it doesn't exist
    }


    for(a in c(1:attachments.obj[['Count']])){ # Loop through attachments
      save.path <- file.path(attach.fldr, attachments.obj[[a]][['FileName']]) # Set the save path
      print(save.path)
      attachments.obj[[a]]$SaveAsFile(save.path) # Save the attachment
    }

  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54444097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档