2017-03-03 104 views
0
library(RDCOMClient) 
## create outlook object 
OutApp <- COMCreate("Outlook.Application") 

我想從名爲'AUX'的Outlook文件夾中檢索今天的電子郵件。 解析電子郵件的標題,如果它符合某些條件,我想解析某些字符串的電子郵件內容。如何使用R RDCOMClient檢索Outlook收件箱電子郵件?

我成功地從R寫了一封電子郵件併發送出去,但至今無法檢索電子郵件。

回答

1

下面是一些示例代碼,我通過反覆試驗工作:

library(RDCOMClient) 

folderName = "AUX" 

## create outlook object 
OutApp <- COMCreate("Outlook.Application") 
outlookNameSpace = OutApp$GetNameSpace("MAPI") 

folder <- outlookNameSpace$Folders(1)$Folders(folderName) 
# Check that we got the right folder 
folder$Name(1) 

emails <- folder$Items 

# Can't figure out how to get number of items, so just doing first 10 
for (i in 1:10) 
{ 
    subject <- emails(i)$Subject(1) 
    # Replace "#78" with the text you are looking for in Email Subject line 
    if (grepl("#78", subject)[1]){ 
    print(emails(i)$Body()) 
    break 
    } 
} 

很抱歉,但我不知道爲什麼有些COM對象的要求參數(如主題(1)),但別人不會(比如Body())。這在Outlook 2013中適用於我,但它也應該適用於2007年以來的所有Outlook版本。

要獲取有關Outlook對象模型的更多信息,我建議您要麼獲得Ken Slovak's Outlook 2007 book(對於更高版本的Outlook仍然相關),要麼檢查我的個人網站http://www.gregthatcher.com(請查看「腳本」部分 - 我)

+0

您好格雷格,$文件夾名稱似乎不是一個適當的R的語法,我省略$符號,但你再次使用它的'文件夾'分配。是否有一個原因? –

+0

「$」是一個錯誤。我編輯了答案。另外,請確保您沒有以管理員身份運行RStudio。您應該使用普通帳戶運行RStudio。 –

+0

你能解釋一下嗎?你在哪裏可以找到讓你知道什麼COM對象可以提取的文檔? –

相關問題