GregK
GregK

Reputation: 81

Sending Email Attachement Through Outlook in R with RDCOMClient

I'm Running a daily analysis that spits out a file I would like sent through my outlook Email. The code I used is featured here, and works wonderfully but the attachment part of it never works...

library(RDCOMClient)


OutApp <- COMCreate("Outlook.Application")


outMail = OutApp$CreateItem(0)

outMail[["To"]] = "[email protected]"
outMail[["subject"]] = "Bruh"
outMail[["body"]] = "Tester"
outMail[["Attachments"]]$Add("L:/Document.csv")

outMail$Send()

The original is here:

Sending email in R via outlook

The code works until the attachment part, and the email even sends, just with no Attachment. It spits this error out:

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

Any Ideas?

Upvotes: 8

Views: 8738

Answers (8)

Sundar
Sundar

Reputation: 1

Here is an example of R code to send an email through OUTLOOK with an ATTACHMENT. has worked so far all day! the key for me has been keeping OUTLOOK open. NOTE: i am not using two slashes etc. Just the simple path that you can copy from the IDE. I am using R STUDIO: "Mountain Hydrangea" Release (583b465e, 2023-06-05) for windows

{r SEND-EMAIL-ON-COMPLETE}
# Install the package
if (!require(RDCOMClient)) 
install.packages("RDCOMClient", repos =
"http://www.omegahat.net/R", type = "win.binary")

# Load the package
library(RDCOMClient)

# Create an email object
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)

# Set the email properties
outMail[["To"]] = "[email protected]"
outMail[["Subject"]] = "R program status"
outMail[["Body"]] = "The program has finished running successfully."

# Add the attachment
#In this code, the file.path() function is used to concatenate the attachment_path and filename to form the full path to the file. This function is platform-independent and will automatically use the correct path separator for your operating system.
# Add the attachment
attachment_path <- "C:/Users/sbak/MY-RESEARCH/MOVIES/MOVIES-ZHHI/"
filename <- "MOVIES-SAStranslate-ZHHI.html"
full_path <- file.path(attachment_path, filename) 
# This will create the full path to the file

outMail[["Attachments"]]$Add(full_path)

# Send the email
outMail$Send()

Upvotes: 0

WimVO
WimVO

Reputation: 116

The problem that our customer was facing was that they executed this operation one too many times.

In your example you are using the filename "L:/Document.csv" over and over again.

Outlook will create that attachment in one of the cache folders each time you try to generate this mail. The first one will be "Document.csv", the second one "Document (002).csv" etc...

We saw it running up to (799) and apparently that is the hard limit.

We had to clean out the temp folder in C:\Users<username>\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook<random hex value> to make it work again.

Just double click an attachment in any mail and see where that temporary file shows up and check that directory.

Upvotes: 0

user3357977
user3357977

Reputation: 1

You can use the gsub() function to change "/" to double back slashes "\" in your path

Use this:

outMail[["Attachments"]]$Add(gsub("/","\\" ,"L:/Document.csv", fixed = TRUE))

Upvotes: 0

Ganesh S
Ganesh S

Reputation: 102

I was also facing the same issue of "Error: Exception occurred".

But, in my case i was missing the naming convention of file. So, make sure that the file name must not be separated by SPACE and use delimiter as "-".

Upvotes: 0

Frameworker247
Frameworker247

Reputation: 107

Reverse the slashes and escape them.

The problem is that the path is being created in R, which prefers forward slashes (since the backslash is the escape character), but it's being interpreted by Outlook, which only takes backslashes.

For example, try adding an attachment to an Outlook email by pasting a path into the insert file dialogue, but change the backslashes to forward slashes. It doesn't accept it. And that's essentially what you're trying to do.

So reverse to make them backslashes, then add extra backslashes to each one to escape them. For example:

C:\\Users\\MyFiles\\Documents\\document.txt

R will strip out the escape characters and and pass a clean path to Outlook.

Upvotes: 6

Ben Jacobson
Ben Jacobson

Reputation: 93

The answer that helped me was provided by David Arenburg in comments:

You need to specify a full path. Is L:/Document.csv a full path? Is L a local driver or you mapped a network driver? If later is the case you need to specify the actual network path.

Example: \\dfwcot\Home$\lando\bb8\2015-12-24 Daily Report.xlsx

Upvotes: 1

Martin
Martin

Reputation: 11

You need to do it like this

L:\\Document.csv

Worked for me. Use two backslashes.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

The Add method of the Attachments class accepts four arguments. I'd suggest specifying them explicitly.

The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment. Make sure the file is accessible.

Upvotes: 0

Related Questions