【问题标题】:RDCOMClient read email from a non-Outlook file pathRDCOMClient 从非 Outlook 文件路径读取电子邮件
【发布时间】:2020-06-06 20:11:21
【问题描述】:

我有一些电子邮件保存在我的 Outlook 目录之外,例如。在某个文件路径"C:\\Users\\foo\\bar.msg"

我想使用library(RDCOMClient) 将这些电子邮件读入 R;在this 问题之后,我已经能够从我的 Outlook 文件夹结构中将电子邮件读入 R。但是考虑到电子邮件的数量,无法将它们导入 Outlook 以便从那里阅读。

this 问题的答案表明,在 VBA 中,您可以使用 OpenSharedItem 从外部文件夹读取电子邮件,但是我无法将其翻译成可在 R 中使用的内容。我的尝试是:

library(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outlookNameSpace <- OutApp$GetNameSpace("MAPI")

message_path <- "C:\\Users\\foo\\bar.msg"
message <- OutApp$OpenSharedItem("message_path")

【问题讨论】:

  • 根据您链接到的文章,您似乎应该通过引用的命名空间对象打开共享项目。由于我没有安装 Outlook,您是否尝试过msg &lt;- outlookNameSpace$OpenSharedItem(..)
  • 嗨 - 谢谢,是的,根据我在下面的回答,我发现这是解决方案。感谢您的观看!

标签: r vba outlook rdcomclient


【解决方案1】:

原来在上面的示例中存在错误的对象引用:在调用 CreateItemFromTemplate 时,我引用了 OutApp 而不是 outlookNamespace

维护问题,因为这可能会节省其他人的搜索并将 VBA 解决方案插入 R。

工作解决方案:

library(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

message_path <- "C:\\Users\\foo\\bar.msg"      
message <- outlookNameSpace$OpenSharedItem(message_path)

【讨论】:

  • 是否需要传递字符串"message_path",而不是变量message_path的值(即字符串"C:\Users\foo\bar.msg")?
【解决方案2】:

您可能根本不需要为此使用RDCOMClient。 hrbrmstr 在他的 github 上有一个名为 msgxtractr 的包,其中包含一个函数 read_msg,该函数将文件路径作为输入并返回一个包含所有消息详细信息的列表对象。

要从 github 安装包,请使用

# install.packages("remotes")
remotes::install_github("hrbrmstr/msgxtractr")
# Alternate GitLab Repo:
# remotes::install_gitlab("hrbrmstr/msgxtractr")

安装包后,您可以使用:

msgxtractr::read_msg("C:\\Users\\foo\\bar.msg")

可能值得将RDCOMClient 解决方案与msgxtractr 进行基准测试。我怀疑RDCOMClient 会慢一些并且可能不太稳定(因为它在应用程序之间进行通信)。

【讨论】:

  • 感谢您的回复。我尝试使用 msgxtractr 作为我的第一种方法,但由于我的系统管理员权限施加的限制,它对我来说不是一个可行的解决方案,所以坚持使用 RDCOMClient。同意这是对没有此类限制的人有用的信息。
  • 如果问题是 https://raw.github.com/* 被阻止(我以前见过),您可以随时去存储库下载 zip,解压缩存储库,然后使用 Rtools 构建包(假设您安装了Rtools)。如果所有 github 都被屏蔽了,那么你也可以试试gitlab repo
  • 嗨 - 这是rtools.exe 的问题,我无法安装它。这也给我带来了问题,之前尝试安装stan
【解决方案3】:

我们也可以使用以下方法提取附件:

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

要提取主语和正文,我们可以使用以下方法:

text_Subject <- message$Subject()
text_Body <- message$Body()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多