I have multiple emails coming in (Each day I get 3 emails for Orders for 3 Categories). The emails subject are in the format:
"ORDERS EXTRACT - [Category] - [Date]".
Where [Category] can be Category 1
, Category 2
or Category 3
. [Date] is the date the email was sent in the format DD/MM/YYYY.
I have a rule setup to search for 'Orders' then call the below code.
I want to run Complete.bat
after all the email attachments have been saved and I only want to call it once.
I've tried to do this by creating another sub called saveAttachtoDisk_CATEGORY1(itm)
that only gets called when it finds "Category 1" in the subject. It then saves the attachment but also searches for a category 1 in the subject AND also searches for yesterday date.
I want a better solution that is not date dependent. A global variable could work where I set the variable to be 1 then run Complete.bat
is sent and then in future if variable = 1 then don't run Complete.bat
. Not sure where to put this variable (Global variable?) As both the sub modules seem the wrong place to put this and reference it.
Both these two modules are saved under the 'Modules' section of Microsoft Outlook VBA.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim SaveFolder As String
SaveFolder = "D:\Orders\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile SaveFolder & "\" & objAtt.DisplayName
objAtt.Delete
Next
itm.Save
End Sub
Other module:
Public Sub saveAttachtoDisk_CATEGORY1(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim SaveFolder As String
SaveFolder = "D:\Orders\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile SaveFolder & "\" & objAtt.DisplayName
objAtt.Delete
Next
itm.Save
If InStr(1, itm.Subject, "ORDERS EXTRACT - Category 1 -" & Format(Date, "dd/mm/yyyy")) Then
Shell "D:\Orders\Complete.bat"
End If
End Sub