0

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")
owen88
  • 454
  • 3
  • 12
  • Based on the article you linked to it looks like you should open a shared item via a referenced namespace object. Since I do not have Outlook installed have u tried `msg <- outlookNameSpace$OpenSharedItem(..)` yet – GWD Feb 22 '20 at 18:00
  • Hi - thanks, yes as per my answer below I spotted that this was the solution. Thanks for taking a look! – owen88 Feb 23 '20 at 10:10

3 Answers3

1

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)
owen88
  • 454
  • 3
  • 12
  • 1
    Is it correct that you need to pass the string `"message_path"`, and not the value of the variable `message_path` (i.e., the string `"C:\Users\foo\bar.msg"`)? – AColeman Feb 23 '20 at 17:12
1

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

AColeman
  • 485
  • 3
  • 8
  • Thanks for your response. I tried using msgxtractr as my first approach, but due to limitations imposed by my system admin privileges its not a feasible solution for me, so stuck with RDCOMClient. Agree that this is useful info for people who don't have such restrictions. – owen88 Feb 23 '20 at 10:09
  • If the issue is that `https://raw.github.com/*` is blocked (which I have seen before), you can always go to the repository and download the zip, the unzip the repository and then build the package using Rtools (assuming you have [Rtools](https://cran.r-project.org/bin/windows/Rtools/) installed). If all of github is blocked, then you could also try the [gitlab repo](https://gitlab.com/hrbrmstr/msgxtractr) – AColeman Feb 23 '20 at 17:15
  • Hi - it's `rtools.exe` that is the problem, I can't install that.This has caused me problems before as well with trying to install `stan`. – owen88 Feb 24 '20 at 07:34
1

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()
Emmanuel Hamel
  • 1,769
  • 7
  • 19