Reputation: 5958
I have several inboxes in Outlook: [email protected], plus a number of shared inboxes like [email protected] for example, or [email protected].
By following this method I am trying to access the emails in my own inbox.
The problem is that sometimes, the inbox accesses mails to [email protected], sometimes it can be any of the others! I've gone through the Omegahat explanations, but their example is mostly focused on excel, and I have no VB experience.
I would like to define which inbox to retrieve the mails from.. My code so far (with the problem of varying inboxes). Cheers.
OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")
folder <- outlookNameSpace$Folders(1)$Folders(folderName)
folder$Name(1)
emails <- folder$Items
for (i in 1:10)
{
subject <- emails(i)$Subject(1)
print(emails(i)$Subject())
}
edit: I am running MSOffice Pro Plus 2016
related: How to use RDCOMClient to send Outlook email from a secondary account - translate existing VBA code?
Upvotes: 3
Views: 2498
Reputation: 107652
Consider Outlook's Stores Object:
OutApp <- COMCreate("Outlook.Application")
OutStores <- OutApp$Session()$Stores()
# 1ST ACCOUNT
myfolder <- OutStores[[1]]$GetRootFolder()$folders(folderName)
# 2ND ACCOUNT
myfolder <- OutStores[[2]]$GetRootFolder()$folders(folderName)
...
Even loop through all stores:
OutApp <- COMCreate("Outlook.Application")
OutStores <- OutApp$Session()$Stores()
store_count <- OutStores$Count()
for (i in 1:store_count) {
myfolder <- OutStores[[i]]$GetRootFolder()$folders(folderName)
emails <- myfolder$Items
for (i in 1:10) {
subject <- emails(i)$Subject()
print(subject)
}
}
# QUIT APPLICATION
OutApp$Quit()
# RELEASE COM RESOURCES
subject <- NULL; emails <- NULL; myfolder <- NULL
OutStores <- NULL; OutApp <- NULL
gc()
Upvotes: 3