This article enables you to set up a trigger in FoxHub based on incoming emails. To help you to get started using FoxHub and understand how the application can help you schedule and orchestrate your bots, please read this article.
Automatically Save Outlook Emails
First, we need to write the relevant VBA code in Outlook. The VBA code will instruct Outlook to save all incoming emails as a file in a specific folder.
While in Outlook, hold down the left ALT key and press F11. This will open the Visual Basic Editor in Outlook.
- Once the VBA editor is open, expand “Project1” in the top left corner and double click on “ThisOutlookSession”.
This will open a new VBA project window into which we will be posting the macro.
- Once the VBA project window is open, copy and paste the following macro into the window:
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Dim objNamespace As Outlook.NameSpace
Set objNamespace = Application.GetNamespace("MAPI")
Set Items = objNamespace.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
SaveMailAsFile Item
End If
End Sub
Private Sub SaveMailAsFile(objMail As Outlook.MailItem)
Dim strFilePath As String
strFilePath = "C:\Program Files (x86)\Foxtrot Suite\"
Dim strFileName As String
strFileName = ReplaceCharsForFileName(objMail.Subject)
strFileName = strFileName & ".msg"
objMail.SaveAs strFilePath & strFileName, olMSG
End Sub
Private Function ReplaceCharsForFileName(ByVal strFileName As String) As String
ReplaceCharsForFileName = ""
strFileName = Replace(strFileName, "/", "")
strFileName = Replace(strFileName, "", "")
strFileName = Replace(strFileName, ":", "")
strFileName = Replace(strFileName, "?", "")
strFileName = Replace(strFileName, Chr(34), "")
strFileName = Replace(strFileName, "<", "")
strFileName = Replace(strFileName, ">", "")
strFileName = Replace(strFileName, "|", "")
ReplaceCharsForFileName = strFileName
End Function
strFilePath = "C:\Program Files (x86)\Foxtrot Suite\" determines where the file will be saved. By default, the file generated by incoming mail will be saved in a Foxtrot folder located on the C:\ drive. In order to change the destination, simply change the path to match the relevant folder (NOTE: it is required to place ”\” at the end of the destination). Make sure that the folder exists before activating the macro.
NOTE: the macro saves only the messages that are received by the default account (in case you have more than one account opened in Outlook).
Outlook settings
Once you have customized the VBA code, you will need to change the security settings in Outlook in order to allow the macro to be executed.
- Press “File” and then “Options”. Then select the bottom option, “Trust Center”.
- Click on ”Trust Center Settings...” and then select ”Macro Settings”. Change the setting to ”Enable all macros”, then restart Outlook.
Once the security settings have been changed, Outlook will now run the macro every time a mail is received. Send yourself a mail in order to make sure that a file is saved in the relevant folder. Do not delete the file, as you will need it later on. Also, create a second folder (in addition to the folder mentioned in the VBA code). This will act as the “Out Folder”.
FoxHub Trigger
The next step is to create the File Trigger in FoxHub.
- Open Fox Hub app and press the “Jobs” button in the bottom left corner.
- Click on the plus button in the top right corner. Choose which type of job you want to add. Give the job a name and select the script you want to run by clicking on the folder button.
- Once the relevant script has been selected, choose the bot that will run the script.
- Once you return to the main window in the Fox Hub app, click on the “Triggers” button in the top left corner of the window.
- Click on the plus button in the top right corner and select “File Trigger”. Give the trigger a name, and then select the job you wish to trigger.
- Press the folder button to the right of the “File” field, and select the file that was generated by the mail you received earlier. You can also type “*.msg”, as it will include all messages in the chosen folder).
- Press the folder button to the right of the “In Folder” and select the folder that you refer to in the VBA code.
- Press the folder button to the right of the “Out Folder” and choose the second folder that you created (the folder that is not mentioned in the VBA code).
Once all of this has been done, the script that was chosen in FoxHub should be activated by sending an email to the specific email account.
Comments
0 comments
Please sign in to leave a comment.