JSR
08-30-2016, 02:12 AM
Good morning everyone,
I'm hoping to get some advice on a section of code, and whether or not it is possible to include a check to prevent the script running twice within a certain time period? I'm a newcomer to VBA and to these forums, so please forgive me if I am making any glaring mistakes. I have assembled/snatched the below code from other help threads here, and the odd tutorial out on the wider web, and it seems to work for everything I need so far:
Option Explicit
Sub AR_EXAMPLE(olItem As MailItem)
Dim vText As Variant
Dim sText As String
Dim sAddr As String
Dim vItem As Variant
Dim i As Long
Dim olOutMail As MailItem
Const strTemplate As String = "file path to email template oft"
With olItem
sText = olItem.Body
vText = Split(sText, Chr(13))
sAddr = "***xx" 'Recipient email address
Set olOutMail = CreateItemFromTemplate(strTemplate)
With olOutMail
.To = sAddr
.Send
End With
End With
lbl_Exit:
Set olOutMail = Nothing
Exit Sub
End Sub
What I am trying to do here:
I work for a company providing appliances to a major chain outlet. These outlets must have working services in order to remain open for business, so it's a large part of our business model to ensure reliable fault reports whenever something goes wrong. Our appliances automatically send out a fault message by email to us whenever something happens, though the message is basic and of no use to the end user directly.
The above code is intended to automatically detect whenever one of these fault emails is received and, depending on the location of the appliance, send a reply using a template to the relevant parties for that store. I cannot use the inbuilt out-of-office reply system for this because the "reply to" address is different from the sender address. I repeat this code many times over with different subroutine names, changing the recipient address depending on the source of the message.
This works perfectly so far for what I need, however I have an issue wherein we sometimes get frequent 'nuisance faults', i.e. multiple fault reports might be received during one single breakdown. The above code will react to each fault report and send another email out to the store, which will quickly get annoying for them and devalue the entire point of the notification. As a solution, I am wondering if there any way to incorporate a check to prevent the above code from running multiple times within a set period, e.g. a day.
Could this be done with a simple IF statement, checking to see if any messages have already been sent to the recipient address before processing the rest of the code, and skipping it if so?
I greatly appreciate any help!
Edit: This is using an Outlook 2016 client on Win 7 (x86) Professional, for a (shared) exchange email account. We have a machine running 24/7 with this email box opened so that the script can always be running to process new messages.
I'm hoping to get some advice on a section of code, and whether or not it is possible to include a check to prevent the script running twice within a certain time period? I'm a newcomer to VBA and to these forums, so please forgive me if I am making any glaring mistakes. I have assembled/snatched the below code from other help threads here, and the odd tutorial out on the wider web, and it seems to work for everything I need so far:
Option Explicit
Sub AR_EXAMPLE(olItem As MailItem)
Dim vText As Variant
Dim sText As String
Dim sAddr As String
Dim vItem As Variant
Dim i As Long
Dim olOutMail As MailItem
Const strTemplate As String = "file path to email template oft"
With olItem
sText = olItem.Body
vText = Split(sText, Chr(13))
sAddr = "***xx" 'Recipient email address
Set olOutMail = CreateItemFromTemplate(strTemplate)
With olOutMail
.To = sAddr
.Send
End With
End With
lbl_Exit:
Set olOutMail = Nothing
Exit Sub
End Sub
What I am trying to do here:
I work for a company providing appliances to a major chain outlet. These outlets must have working services in order to remain open for business, so it's a large part of our business model to ensure reliable fault reports whenever something goes wrong. Our appliances automatically send out a fault message by email to us whenever something happens, though the message is basic and of no use to the end user directly.
The above code is intended to automatically detect whenever one of these fault emails is received and, depending on the location of the appliance, send a reply using a template to the relevant parties for that store. I cannot use the inbuilt out-of-office reply system for this because the "reply to" address is different from the sender address. I repeat this code many times over with different subroutine names, changing the recipient address depending on the source of the message.
This works perfectly so far for what I need, however I have an issue wherein we sometimes get frequent 'nuisance faults', i.e. multiple fault reports might be received during one single breakdown. The above code will react to each fault report and send another email out to the store, which will quickly get annoying for them and devalue the entire point of the notification. As a solution, I am wondering if there any way to incorporate a check to prevent the above code from running multiple times within a set period, e.g. a day.
Could this be done with a simple IF statement, checking to see if any messages have already been sent to the recipient address before processing the rest of the code, and skipping it if so?
I greatly appreciate any help!
Edit: This is using an Outlook 2016 client on Win 7 (x86) Professional, for a (shared) exchange email account. We have a machine running 24/7 with this email box opened so that the script can always be running to process new messages.