# StudioX - Filtering unread emails by subject heading, copying data from xls att for processing

I have a “For Each Email” Activity looking at the inbox and limiting to 3 emails (for testing) and set for only unread mail.

I then have an IF condition filtering current mail based on Contains string of Subject Heading.
If passed, then:

• Saves attachment
• Copies data for each row into another spreadsheet by row
• saves target xlsx file

The issue is that based on the For Each Email Activity Limit emails to first setting (set to 3), it just copies the data from the first email attachment xls 3 times into the target xlsx when it should be looking at 3 separate unread emails (and attachments) that match subject heading and writing data from each file into the target file.

At the end of the day, I’m trying to filter emails by subject and copy PO #'s from single attachment into a target xlsx for collection and final processing.

Help is appreciated!! Screenshot below

Can you verify that the subject line on each of your test emails matches exactly with your condition?
When testing your code, I observed that the If condition to match text is case-sensitive.
I tested on 3 emails with following subject lines:

1. Test Email
2. test 2
3. test 3

If [CurrentMail] Subject Contains test
it only stores the files from the second and third emails.
If I don’t put a condition, all three are saved.

Yes thanks, the 1st and 3rd email are exactly the same except for different PO #'s in attachment. 2nd unread email is not a match on purpose for testing.

So it is only saving the data from the first email 3x in a loop to the target instead of saving data from the two emails. Like it is counting 3 unread emails using my limits but only taking data from the 1st one 3 times.

@gregory.hensley
Do you get 3 copies of the same files?

Hi Yes I do get 3 copies of the same files. I know this because the sample PO #'s I am extracting are different for each file. I get the same 2 PO#'s copies 3 times. There should only be 2 emails and 2 files but the filter isn’t working.

Summary:
2 should meet filter requirements (limit set to 3 emails unread)
2 files should be downloaded and 2 PO#'s for each file should be extracted.
I am actually only getting the first same file downloaded 3 times (matches limit)
The other file from the other good email is not being downloaded.

Still cannot figure this out. Thanks in advance.

Here is the full flow I have in case that helps. Thanks again

@gregory.hensley
Can you please share an individual screenshot of each test email (showing attachments)?

Thanks.

Hi,

Both emails are exactly the same

Attachment looks like this

Target file looks like this:

I am making progress with some consistency.

Seem now the reason the overwrite of the same PO #'s is due to the Use Excel File being a specific file PO Details.xls. When there’s more than one email, the save attachment part of the loop sees an existing file and saves the next as PO Details_1.xls then PO Details_2.xls. Of course the loop doesn’t know to open PO Details_1.xls etc.

On use Excel File activity, I deselected “CreateNewFile” so it will overwrite the file but that doesn’t seem to be working. It still creates the versions.

I also tried a delete file at the end of the for each email loop but it won’t delete because it is already open. I cannot find a way to close the PO Details.xls file and delete it so it can be overwritten. Seems I am making this more difficult that should be. I feel the solution is close. Any ideas?

In your For Each loop (image below) change the Save to Folder to something dynamic.
For example, have a unique value from email or simply a counter variable and increment it at the end of the loop.
Use something like Project Folder\Files\in\(counter)

This way you will have one download folder per email and the files won’t need to be overwritten.
Access your files with the same way they were saved and each loop will get you exactly the file you need.

@codenameCata
Thanks for your help in working with me to think this through!
The final solution was to place a Delete File activity at the end of the For each email activity. I had it in the Use Excel File activity.

1 Like