In this tutorial, we will create an automation that saves the attachments to all the emails in an Outlook Search Folder to your computer, then moves the files to another folder on your computer and renames them by adding today's date to the file name. To rename the files, we will use a formula in the default Excel Workspace available in StudioX.
We will create a project and start by adding an Outlook Account Card to indicate the Outlook account to use. In the card, we will add a For Each Mail Message activity and then the common scenario Iterate through mail attachments to indicate that we want to save the attachments for each email and then process each file after it is saved. Iterating through each file in the folder where the attachments are saved, we will then add a Write Cell activity to copy the file name and edit the name in the Excel Workspace, and a Move File activity to move the file with the updated name to another folder.
Step 1: Create two folders on your computer and a Search Folder in Outlook.
- Create two folders named In and Out at the root of the C: drive on your computer.
- Open Outlook, then right-click Search Folders in the Folder pane and select New Search Folder.
- In the New Search Folder window, select Create a custom Search Folder > Choose.
- In the Custom Search Folder window, enter "Last week's attachments" in the Name box and then select Criteria.
- In the Messages tab of the Search Folder Criteria window, from the drop-down menus next to Time select received and Last Week.
- From the More Choices tab, select Only items with and then one or more attachments from the adjacent drop-down menu.
- Click OK three times to close the windows and save the search folder.
Step 2: Set up a project.
Step 3: Add the Outlook account to the project.
- In the Activities panel, select the Outlook tab, and then double-click Outlook Account Card. An Outlook Account Card activity is added to the Designer panel.
- In the activity, the default email account is already selected in the Select mail account field. If you want to use a different account, select it from the drop-down menu.
In the And reference it later as field, leave the default valueOutlook
as the name under which to refer to the account in the automation.
Step 4: Iterate through the messages in the Outlook Search Folder.
- In the Activities panel, drag the For Each Mail Message activity and drop it inside the Outlook Account Card.
- In the For Each Mail Message activity, click on the right side of the Use mails from field, and then select Outlook > [Search Folders] > [Last week's attachments]. Your selection is displayed in the field as
Last week's attachments
.
You have indicated that you want to iterate through the emails in the Outlook folder "Last week's attachments".
Step 5: Iterate through the mail attachments.
- Click inside the For Each Mail Message activity and then, in the search box at the top of the screen, select the common scenario Iterate through mail attachments.
- Browse to and select the In folder and click OK.
Two activities are added to your project:- Save Mail Attachments with
Current Mail
selected for the From Message field, indicating that the attachments from the current mail in the iteration should be saved to the In folder that is indicated by theC:\In
selected in the Save to folder field. - For Each File In Folder with
C:\In
selected in the Folder field to indicate that you will iterate through the files in this folder.
If you click Start to execute the automation now, the mail attachments are downloaded and saved to the In Folder.
- Save Mail Attachments with
Step 6: Rename and move the mail attachments to a different folder.
- In the Activities panel, select the Excel tab, then drag the Write Cell activity and drop it inside the For Each File In Folder activity.
- In the Write Cell activity:
- Click on the right side of the What to write field and then select Current File > Full Path to indicate that we want to copy the full path of the current file in the iteration.
- Click on the right side of the Where to write field and then select Workspace Excel > File [Sheet] > FullFileName [Cell] to indicate that we want to paste the full path of the current file in the iteration to the FullFileName named cell in the File sheet of the Excel Workspace.
- Click Open Excel Workspace in the ribbon menu. In the Excel_Workspace file, go to the File sheet, update the value in the named cell
ReformattedFileName
(B14) to the following:=FileNameNoExtension & "-" & YYYYMMDD & "." & FileExtension
and save the file. This formula adds the current date at the end of the file name. - In the Activities panel, select the File tab, then drag the Move File activity and drop it after the Write Cell activity inside For Each File In Folder.
- In the Move File activity:
- Click on the right side of the From field and then select Current File > Full Path to indicate the full path of the current file in the iteration.
- Click on the right side of the To field and then select Text. In the Text Builder, enter
C:\Out\
and then click on the right side of the window and select Workspace Excel > File [Sheet] > ReformattedFileName [Cell]. The path in the Text Builder should look like this:C:\Out\[Excel] File!ReformattedFileName
. - Make sure the Overwrite option is selected.
- Click Save in the Text Builder.
- Click Save in the ribbon menu to save the automation, then click Run to execute the automation.
The attachments from each email in the Outlook Search Folder are saved to the In folder on the computer, then today's date is added to the name and they are moved to the Out folder.
Updated 2 years ago