Get Attachment Names

Dear Forum Members,

I have designed a workflow in which I am extracting the data from Gmail account and saving the attachment, and writing all the extracted data into excel. For eg. Excel file can contain, Name, emailid, body of email, Date and Attachment Name. So, bot will extract the data and write into excel. If it has attachment, it will write the name of the attachment in that column else, it will leave the field blank.

Now, my question is: My bot is extracting only first attachment name, but what if the mail has multiple attachments. Right now, I am using “CType(mail.Attachments.FirstOrDefault,Attachment).Name” , this code for getting the name of the first Attachment.

I have also tried “mailMessage.Attachments.Select(function(x) x.Name)”, this code for getting all the Attachment Names but this code is not extracting any value in the variable.

The attachments which are saving in a folder, I can not use Directory.Getfiles(foldername) because the folder can have previous files as well, so if I use this method so with each iteration, it will give the names of unwanted files as well. I want the bot should pick the filenames as the attachments got saved.

I want to get names of the attachments in a variable, whether it is single attachment or multiple attachments. How can I achieve this.

Any help will be appreciated. Thanks in advance.

This code is correct and should return collection of attachment names
image

You will get list of attachments in a variable of ienumerate(of string) data type

Cheers

@J0ska,

Thanks for your response. I tried the above code and getting “System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Net.Mail.Attachment,System.String]” this as Attachment Name. I don’t know, where I am wrong.

Please help with this. Thanks

As stated above - the expression will return ienumerable(of string) data type
Looks like you are assigning the result of the expression to a string type variable
You need to create a variable of ienumerable(of string) datatype - see picture in prev post

ltsAttachments = mailMessage.Attachments.Select(function(x) x.Name)

Cheers

@J0ska,

I am not using the string data type. I am using the variable of IEnumerable type. I have debugged the code, in the variable, I am getting attachment names but as I try to write into excel, I am getting this System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Net.Mail.Attachment,System.String] uipath, in attachment name column.

Please help with this, how can I write the data into excel, Other column names are of type string and this attachment name is of type Enumerable. Thanks

@dimple.khurana
lets assume you want to get a string. give a try on:
myAttachements = String.Join(“,”, mailMessage.Attachments.Select(function(x) x.Name).toArray)

1 Like

Okay. I did not get you want to write attachment names in the excel.
@ppr gave you the right answer.

Cheers

@ppr,

Thanks a lot for your help. I got the expected result.

@J0ska,

Thanks a lot for your help. I got the expected result.

@dimple.khurana
Perfect. it is working.

May I ask you to set the solution flag to @J0ska as he gave the main building block for this. Thanks

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.