Using RDCOMClient to search Outlook inbox

烈酒焚心 提交于 2019-12-08 07:21:09

问题


I'm attempting to use RDCOMClient to search my Outlook inbox for specific subjects in emails and then grab the attachment. I have this working on a single email, but as the subject contains a date element, I need the search to be a like clause, but can't quite see where this would fit within my below query.

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

I need to search just the first portion of the subject line, looking for everything before the date and time.


回答1:


I think something like this should work. It should search for any messages containing the specified phrase, and download each of their attachments.

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
    }

  }
}


来源:https://stackoverflow.com/questions/54444097/using-rdcomclient-to-search-outlook-inbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!