Reputation: 474
I have a number of emails saved outside my Outlook directory, eg. at some file path "C:\\Users\\foo\\bar.msg"
.
I would like to read these emails into R using library(RDCOMClient)
; following this question I have been able to read emails into R from my Outlook folder structure. However given the volume of emails it is not possible to import them into Outlook to read from there.
The answer to this question suggests that within VBA you can use OpenSharedItem to read emails from an external folder, however I haven't been able to translate this into something that works in R. My attempt is:
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace <- OutApp$GetNameSpace("MAPI")
message_path <- "C:\\Users\\foo\\bar.msg"
message <- OutApp$OpenSharedItem("message_path")
Upvotes: 0
Views: 489
Reputation: 2213
We can also extract the attached files using the following approach :
library(RDCOMClient)
destination_dir <- "C:\\Users"
path_msg <- "C:\\Users\\foo\\bar.msg"
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace <- OutApp$GetNameSpace("MAPI")
message <- outlookNameSpace$OpenSharedItem(path_msg)
nb_Attached_Files <- message$Attachments()$Count()
list_Attached_Files <- list()
for(i in 1 : nb_Attached_Files)
{
print(i)
attachment_file <- paste0(destination_dir, .Platform$file.sep, message$Attachments(i)$Filename())
list_Attached_Files[[i]] <- attachment_file
message$Attachments(i)$SaveAsFile(attachment_file)
}
To extract the subject and the body, we can use the following approach :
text_Subject <- message$Subject()
text_Body <- message$Body()
Upvotes: 1
Reputation: 474
Turned out there was a wrong object reference in my example above: I reference OutApp
rather than outlookNamespace
, when calling CreateItemFromTemplate
Maintaining the question as this might save somebody else the search and interpolating the VBA solution into R.
Working solution:
library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")
message_path <- "C:\\Users\\foo\\bar.msg"
message <- outlookNameSpace$OpenSharedItem(message_path)
Upvotes: 1
Reputation: 555
You may not need to use RDCOMClient
at all for this. hrbrmstr has a package on his github called msgxtractr which contains a function read_msg
that takes as input a file path and returns a list object with all of the details of the message.
To install the package from github, use
# install.packages("remotes")
remotes::install_github("hrbrmstr/msgxtractr")
# Alternate GitLab Repo:
# remotes::install_gitlab("hrbrmstr/msgxtractr")
Once the package is installed, you could use:
msgxtractr::read_msg("C:\\Users\\foo\\bar.msg")
It's probably worth benchmarking the RDCOMClient
solution against msgxtractr
. I suspect that RDCOMClient
will be a bit slower and probably less stable (since it's communicating between applications).
Upvotes: 1