NOTE: Instead of writing your own VBScript code, we do recommend considering using our developed solution for working with Outlook and performing email commands in the application.
In some cases, it is not possible to use the email actions in Foxtrot as it is either not accessible or simply not allowed to access the email server via SMTP (to send) or IMAP (to receive) emails. If this happens, the UI can be used to automate the process in the Outlook application via Foxtrot's other actions. But, this method can be complex and can lead to unsustainable results. Instead, if you have programming experience, we recommend using the VBScript action to call the Outlook object to do all the commands that you would otherwise use the email actions in Foxtrot for.
- For general information on the VBScript action in Foxtrot, please read this article.
In the attached script at the bottom of the article, you can find inspiration on how to automate email processes in Outlook using the VBScript action of Foxtrot. The script is based on you aiming to work with emails in the default account of the Outlook application in the default Inbox folder. All processes are different, therefore, you should expect to have to modify and adjust the solution to your needs. If you do not have any VBScript, VBA, or other programming experience, this will be very advanced for you. If you use Office365, please see this guide. Also, in the Outlook application, you should enable and allow scripting like shown in this guide. The attached script shows you how you can:
- Send Emails
- Read Emails
- Save Attachments
- Save Email as a .msg File
- Reply Emails
- Forward Emails
- Move Emails
- Delete Emails
We will briefly cover the script part by part. The script uses Foxtrot variables to define the information it will use when performing the commands in the VBScript action. Conceptually, you could also simply write one long VBScript command to perform multiple actions without using any Foxtrot variable if that suits your needs better. This script is broken up into parts to better illustrate the different capabilities.
Send Emails
All necessary information, including copies, hidden copies, and attachments, is set using variables, which they are implemented into the script, using RPAEngine.GetVar function.
NOTE: Variable “Attachments” must include the full path to the files and file extensions.
It is also considered best practice when scripting to increase the script readability by naming the strings like “strVariableName”, objects like “objObjectName” and so on.
Dim objOutlook, objEmail
Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(0)
Dim strEmailReceiver, strEmailCc, strEmailBcc, strEmailSubject, strEmailBody, strEmailAttachments
RPAEngine.GetVar "EmailReceiver", strEmailReceiver
RPAEngine.GetVar "EmailCc", strEmailCc
RPAEngine.GetVar "EmailBcc", strEmailBcc
RPAEngine.GetVar "EmailSubject", strEmailSubject
RPAEngine.GetVar "EmailBody", strEmailBody
RPAEngine.GetVar "EmailAttachments", strEmailAttachments
With objEmail
.To = strEmailReceiver
.Cc = strEmailCc
.Bcc = strEmailBcc
.Subject = strEmailSubject
.Body = strEmailBody
If (strEmailAttachments <> "") Then
.Attachments.Add strEmailAttachments
End If
.Send
End With
'Clear the memory
Set objOutlook = Nothing
Set objEmail = Nothing
NOTE: Variable “Attachments” must include the full path to the files and file extensions.
It is also considered as a good manner of scripting to increase the script readability by naming the strings like “strVariableName”, objects like “objObjectName” and so on.
Incoming Emails
First, in order to work with incoming emails, you should implement a similar solution as the Get Email action in Foxtrot by retrieving all emails in a specific folder of the account. Thereafter, you can then read the content of the email, move it, etc. depending on the process. The first VBScript action will simply detect the total number of emails in the given folder. It defaults to the inbox of the account, but it is possible to specify a different folder if needed.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(6)
Dim strFolder
RPAEngine.GetVar "EmailsFolder", strFolder
If (strfolder <> "Inbox") Then
Set objFolder = objFolder.Folders(strFolder)
End If
RPAEngine.SetVar "Emails", objFolder.Items.Count
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objFolder = Nothing
Read Emails
Hereafter, based on the total number of emails in the specific folder, the script moves into a loop to read all the emails in the folder. Variables will be created for each loop, however, you could also add the data into a list instead of variables - that is up to you. This VBScript action will retrieve all specified information from the current email in the loop and store it in variables.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(6)
Dim strFolder
RPAEngine.GetVar "EmailsFolder", strFolder
If (strfolder <> "Inbox") Then
Set objFolder = objFolder.Folders(strFolder)
End If
Dim colItems
Set colItems = objFolder.Items
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail
Set objEmail = colItems(colItems.Count - (intEmail - 1))
RPAEngine.SetVar intEmail & "EmailID", objEmail.EntryID
RPAEngine.SetVar intEmail & "EmailDate", objEmail.ReceivedTime
RPAEngine.SetVar intEmail & "EmailUnread", objEmail.UnRead
RPAEngine.SetVar intEmail & "EmailSender", objEmail.Sender & " (" & objEmail.SenderEmailAddress & ")"
RPAEngine.SetVar intEmail & "EmailCc", objEmail.Cc
RPAEngine.SetVar intEmail & "EmailSubject", objEmail.Subject
RPAEngine.SetVar intEmail & "EmailBody", objEmail.Body
RPAEngine.SetVar intEmail & "EmailBodyHTML", objEmail.HTMLBody
RPAEngine.SetVar intEmail & "EmailAttachments", objEmail.Attachments.Count
Dim intAttachment, strAttachments
For intAttachment = 1 to objEmail.Attachments.Count
If (intAttachment = 1) Then
strAttachments = objEmail.Attachments.Item(intAttachment).DisplayName
Else
strAttachments = strAttachments & ", " & objEmail.Attachments.Item(intAttachment).DisplayName
End If
Next
RPAEngine.SetVar intEmail & "EmailAttachmentNames", strAttachments
'Clear the memory
Set objOutlook = Nothing
Set objEmail = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set colItems = Nothing
An important variable is the EmailID (objEmail.EntryID), that is the unique ID of the email and that is relevant for future reference to make sure that the script will actually handle the correct email in case a new email has arrived during the execution of the script.
Download Emails and Attachments
After reading all the content of the email, it is possible to download the email itself and the attachments of the email. Notice how the following scripts use the EmailID variable to find the correct email.
Download the email as .msg:
Dim objOutlook, objNamespace
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
Dim objFileSystem, blnFilePathExists
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim strFilePath, strFileName
RPAEngine.GetVar "FolderDestinationAttachmentDownload", strFilePath
On Error Resume Next
objFileSystem.CreateFolder(strFilePath)
objFileSystem.CreateFolder(strFilePath & "\" & strEmailID)
On Error GoTo 0
RPAEngine.GetVar intEmail & "EmailSubjectClean", strFileName
objEmail.SaveAs strFilePath & "\" & strEmailID & "\" & strFileName & ".msg"
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set objAttachments = Nothing
Set objFileSystem = Nothing
Download the attachments:
Dim objOutlook, objNamespace
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
Dim objFileSystem, blnFilePathExists
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Dim strFilePath, strFileName
RPAEngine.GetVar "FolderDestinationAttachmentDownload", strFilePath
On Error Resume Next
objFileSystem.CreateFolder(strFilePath)
On Error GoTo 0
Dim objAttachments
Set objAttachments = objEmail.Attachments
Dim intAttachmentCount
intAttachmentCount = objAttachments.Count
Dim intAttachmentCurrent
For intAttachmentCurrent = 1 to intAttachmentCount
strFileName = objAttachments.Item(intAttachmentCurrent).FileName
On Error Resume Next
objFileSystem.CreateFolder(strFilePath & "\" & strEmailID)
On Error GoTo 0
strFileName = strFilePath & "\" & strEmailID & "\" & strFileName
objAttachments.Item(intAttachmentCurrent).SaveAsFile strFileName
Next
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set objAttachments = Nothing
Set objFileSystem = Nothing
Reply Emails
If you wish to reply to the email, you can use the following script.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
Dim objEmailReply
Set objEmailReply = objEmail.ReplyAll
Dim strEmailBody, strEmailAttachments
RPAEngine.GetVar "EmailBody", strEmailBody
RPAEngine.GetVar "EmailAttachments", strEmailAttachments
With objEmailReply
.HTMLBody = strEmailBody & vbCrLf & .HTMLBody
If (strEmailAttachments <> "") Then
.Attachments.Add strEmailAttachments
End If
.Send
End With
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set objEmailReply = Nothing
Forward Emails
If you wish to forward the email, you can use the following script.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
Dim objEmailForward
Set objEmailForward = objEmail.Forward
Dim strEmailReceiver, strEmailCc, strEmailBcc, strEmailBody
RPAEngine.GetVar "EmailReceiver", strEmailReceiver
RPAEngine.GetVar "EmailCc", strEmailCc
RPAEngine.GetVar "EmailBcc", strEmailBcc
RPAEngine.GetVar "EmailBody", strEmailBody
With objEmailForward
.To = strEmailReceiver
.Cc = strEmailCc
.Bcc = strEmailBcc
.HTMLBody = strEmailBody & vbCrLf & .HTMLBody
.Send
End With
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set objEmailForward = Nothing
Move Emails
If you wish to move the email, you can use the following script.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(6)
Dim strFolderSub
RPAEngine.GetVar "EmailMoveToFolder", strFolderSub
Dim objFolderSub
Set objFolderSub = objFolder.Folders(strFolderSub)
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
objEmail.Move objFolderSub
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
Set objFolder = Nothing
Set objFolderSub = Nothing
Delete Emails
If you wish to delete the email, you can use the following script.
Dim objOutlook, objNamespace, objFolder
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Dim intEmail
RPAEngine.GetVar "CurrentEmail", intEmail
Dim objEmail, strEmailID
RPAEngine.GetVar intEmail & "EmailID", strEmailID
Set objEmail = objNamespace.GetItemFromID(strEmailID)
objEmail.Delete
'Clear the memory
Set objOutlook = Nothing
Set objNamespace = Nothing
Set objEmail = Nothing
What else?
You can basically do anything in Outlook using VBScript. You could also mark emails as read/unread, etc. Simply research the internet on how to do your desired command in Outlook using VBScript or VBA.
Comments
0 comments
Please sign in to leave a comment.