-->

Sending Email Attachement Through Outlook in R wit

2019-04-07 15:55发布

问题:

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"]] = "gkinne@horizonmedia.com"
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?

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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 "-".



回答5:

You need to do it like this

L:\\Document.csv

Worked for me. Use two backslashes.