0

Is it possible to write an r code that would auto update how many emails were in my outlook subfolder XYZ? I need to use that number (it'll count the number of bounced emails) as a variable in my code.

I've never integrated R and outlook and am not really sure where to start. Any pointers/ sample code would be appreciated!!

Ellie
  • 415
  • 7
  • 16
  • You can use R to call a VBA script, which can directly reference MSOffice objects: [example](https://stackoverflow.com/a/37086738/4421870). I think you can just look up a VBA solution for counting emails, then call that within R. – Mako212 Aug 27 '18 at 15:24
  • Using package `RDCOMClient` I think you could manage something : http://www.omegahat.net/RDCOMClient/ . In this link they mention only excel but I used it to interact with powerpoint so outlook shouldn't be so different. – moodymudskipper Aug 27 '18 at 15:29
  • @Ellie did you manage to get what you wanted ? – moodymudskipper Sep 02 '18 at 07:52

1 Answers1

4

The library RDCOMClient will be useful for you, here's what I could do on my side:

library(RDCOMClient)
ol <- COMCreate("outlook.Application")
objFolder <- ol$ActiveExplorer()$CurrentFolder()
objFolder$Items()$Count()
# [1] 489

The way it works is not straightforward and the doc of the package is not super easy, but here's how I find my way around it :

Use the VBA reference : https://learn.microsoft.com/en-us/office/vba/api/outlook.itemproperties

It gives you the object structure, it's sometimes easier to google than to navigate it directly.

Find out how people do it in regular VBA, and translate it in RDCOMClient syntax.

For example for this case I found on this link the following vba lines of code :

Set objFolder = Application.ActiveExplorer.CurrentFolder
EmailCount = objFolder.Items.Count

And translated them as shown above.

With a bit of trial and error, looking for existing code and tweaking with the vba reference you will hopefully find your way!

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167