1

I am trying to query Outlook Contacts and extract them all in a csv file using R programming language. I have a example piece of code:

#####################################################
library(RDCOMClient)
## init com api
OutApp <- COMCreate("Outlook.Application")
olFolderContacts <- 10

# get the namespace
ns <- OutApp$GetNamespace("MAPI")
# get the contact folder
folderContacts = ns$GetDefaultFolder(olFolderContacts)
# get the contact list
folderItems = folderContacts$Items()
i = 0
# get the first contact
itemObj = folderItems$Item(i+1)
#####################################################
# That's here that it fails
#####################################################
ContactName <- itemObj$GetElement()$Name()
#####################################################

The idea is that the folder contains items which are of different type. How to query the Object COM interface to get the object type or Class and how to invoke the methods related to it?

Thanks,

Maurizio.

gaut
  • 5,771
  • 1
  • 14
  • 45
MNapoli
  • 40
  • 6
  • 1
    welcome to SO ;) – gaut Apr 29 '21 at 12:20
  • please describe the error you get? – gaut Apr 30 '21 at 08:17
  • I managed to fix it... – MNapoli May 04 '21 at 11:20
  • Here is the code: – MNapoli May 04 '21 at 11:20
  • `code` library(RDCOMClient) ## init com OutApp <- COMCreate("Outlook.Application") olFolderContacts <- 10 # get the namespace ns <- OutApp$GetNamespace("MAPI") # get the contact folder folderContacts = ns$GetDefaultFolder(olFolderContacts) # get the contact list folderItems = folderContacts$Items() i <- 0 for (i in 1:folderItems$Count()){ itemObj = folderItems$Item(i) ContactFirstName <- itemObj$firstname() ContactLastName <- itemObj$lastname() ContactEmail <- itemObj$email1address() print( paste( ContactEmail, ContactFirstName, ContactLastName, sep = " - ")) } – MNapoli May 04 '21 at 11:20
  • it's better if you answer your question: https://stackoverflow.com/help/self-answer#:~:text=If%20you%20have%20more%20than,own%20question%20at%20any%20time. – gaut May 04 '21 at 12:19
  • also code in comments can be done with ` – gaut May 04 '21 at 12:19

1 Answers1

0

I have been able to extract the list of my contacts with the following code :

library(RDCOMClient)
Application <- RDCOMClient::COMCreate("Outlook.Application")
ns <- Application$GetNamespace("MAPI") 
foldContact <- ns$GetDefaultFolder(10) 
colItems <- foldContact$Items()$Restrict("[MessageClass]='IPM.Contact'") 
nb_Contact <- colItems$Count()
list_Info_Contact <- list()

for(i in 1 : nb_Contact)
{
  creation_Time <- colItems[[i]]$CreationTime()
  full_Name <- colItems[[i]]$FullName() 
  list_Temp_Info <- list(creation_Time = creation_Time, full_Name = full_Name)
  list_Info_Contact[[i]] <- list_Temp_Info
}
Emmanuel Hamel
  • 1,769
  • 7
  • 19