2

I am trying to get the name of a attachment in the mail using RDCOM client in r.

i am able to get the name of subject and also the text in the body

But i am not able to figure out how to get the name of attachment

OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")
folder <- outlookNameSpace$Folders(1)$Folders(1)
emails <- folder$Items
emails(1)[['Subject']] #Gives me name of subject
emails(1)[['body']] # give me text in body of the mail
emails(1)[['attachments']] # Doesn't give me text. It gives me a pointer like 
below

An object of class "COMIDispatch"
Slot "ref":
<pointer: 0x0000000008479448>

Can anyone help me with this problem?

2 Answers2

2

The following will create a vector containing the names of all the attachments on an email.

library(RDCOMClient)

OutApp <- COMCreate("Outlook.Application")
outlookNameSpace = OutApp$GetNameSpace("MAPI")

folder <- outlookNameSpace$Folders(1)$Folders(1)

emails <- folder$Items
emails(1)[['Subject']] #Gives me name of subject
emails(1)[['body']] # give me text in body of the mail

attachments.obj <- emails(1)[['attachments']] # Gets the attachment object
attachments <- character() # Create an empty vector for attachment names

if(attachments.obj$Count() > 0){ # Check if there are attachments
  for(i in c(1:attachments.obj$Count())){ # Loop through attachments
    attachments <- append(attachments, attachments.obj$Item(i)[['DisplayName']]) # Add attachment name to vector
  }
}

print(attachments)
Matt Jewett
  • 3,249
  • 1
  • 14
  • 21
0

The Attachments property of the MailItem class returns an Attachments object that represents all the attachments for the specified item. That is a collection.

I am not familiar with R syntax, so I will paste here a C# sample code:

for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
   newEmail.Attachments[i].SaveAsFile(@"C:\TestFileSave\" +
    newEmail.Attachments[i].FileName);
}
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45