Automatically Download and Overwrite File Based on Specific Text From Email

Hello everyone,

This is my first post and I am very new to R so please excuse any inconsistencies in my explanations.

Here's the scenario:

I am trying to download a .txt attachment from Outlook that gets automatically emailed to me every day, convert it to .xlsx, name it, and replace/overwrite the file from the day before.

I cannot modify the subject line or the file name, so I am hoping to this based on specific text from the email.

Below is my best attempt at it. When I go to run it, RStudio experiences a fatal error and crashes...

devtools::install_github("omegahat/RDCOMclient")
require(RDCOMClient)
require(readtext)
require(rio)
require(readr)

outlook_app <- COMCreate("Outlook.Application")
search <- outlook_app$AdvancedSearch(
"Inbox",
"urn:schemas:httpmail:textdescription like 'Student Demographics - Dashboard'"
)
Sys.sleep(15)

results <- search$Results()

for (i in 1:results$Count()) {
if(as.Date(floor(results$Item(i)$ReceivedTime()), origin = "1899-12-30") == Sys.Date()) {
email <- results$Item(i)
}
}

attachment_file <- "G:/My Drive/Dashboards.txt"
email$Attachments(1)$SaveAsFile(attachment_file)

I am using version 4.1.1 in my attempt.

Any help you can provide is greatly appreciated. Thank you for your time!

Hi,

Welcome to the RStudio community!

In order for us to help you with your question, please provide us a minimal reproducible example where you provide a minimal (dummy) dataset and code that can recreate the issue. Once we have that, we can go from there. For help on creating a Reprex, see this guide:

In this case, you did share code, but we cannot run this because we have no access to your emails. Also, if there is an error it would be nice to see the full error message.

At which step does your code crash? It it when trying to get the email, or during the processing. Also, if possible, try and show what goes into the "search" variable

Good luck!
PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.