2

I have several inboxes in Outlook: my.name@abc.com, plus a number of shared inboxes like team.data@abc.com for example, or team.ba@abc.com.

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 my.name@abc.com, 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?

gaut
  • 5,771
  • 1
  • 14
  • 45
  • 1
    To find a specific outlook subfolder, you can start by finding the right account using `folder <- outlookNameSpace$Folders(i); folder$Name(1)` and for i=1:n, then say i=2 is the right account, `folder <- outlookNameSpace$Folders(2)$Folders(j); folder$Name(1)` for j=1:m for the right folder (say j=6), then `folder <- outlookNameSpace$Folders(2)$Folders(6)$Folders(k); folder$Name(1)` for the right subfolder. – Mark Neal Jan 07 '20 at 00:45
  • Thank you Mark. Is this documented somewhere? – gaut Jan 10 '20 at 10:54
  • Not well-documented! Sorry, can't find where the hints were that I picked up on. Basically only a few key folders have names (e.g. inbox), and so everything else is numbered. – Mark Neal Jan 23 '20 at 02:42
  • 1
    For posterity, I’ll note you can run into trouble with hard coding the index numbers into your code. On our email system, perhaps due to system upgrades, the index numbers that describe my target folder changed. – Mark Neal Mar 14 '20 at 20:05

1 Answers1

2

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()
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Many thanks for your answer, I am having the following error when running this code: Error in OutStores(1) : could not find function "OutStores" but on the other side store_count does give me a number of 6 – gaut Oct 05 '18 at 06:55
  • 1
    See edit. Since *OutStores* is now an R object, it must be indexed with brackets method: `[[i]]`. – Parfait Oct 05 '18 at 13:44
  • Many thanks, just one more question, how can I verify the name of the current store? eg. my.name@abc.com or team.ba@abc.com. I tried with OutStores[[3]]$Name(1) , got the message: Cannot locate 0 name(s) Name in COM object – gaut Oct 08 '18 at 09:43
  • 1
    Add GetRootFolder: `OutStores[[3]]$GetRootFolder()$Name()` – Parfait Oct 08 '18 at 14:23