PDA

View Full Version : VBA - auto reply to email based on limit in body of email



shadyg89
12-21-2015, 11:24 PM
Hi guys,

I need help creating a VBA code to send an automatic reply when a specific value in the body of the email reaches a certain limit (trigger level).
The email i receive look like this and I would like it to send an email if the Peak values are above 2.000 mm/s (therefore would only like it to scan the Peak Row (Fourth Row)). If the limit is above I would like it to send a quick email saying Alert Level with The first Row in the subject lines. If you can please help me with this that would be amazing.

Thank you

Dec 14 /15 19:05:21, BEXXXXX, M123456

Chan Tran Vert Long
Peak 0.381 mm/s 1.90 mm/s 0.889 mm/s
Accel 0.0398 g 0.0398 g 0.0398 g
Disp 0.00322 mm 0.0220 mm 0.0105 mm
Freq 51 Hz. 18 Hz. 14 Hz.
Mic 88.0 dB(L)
PVS 1.93 mm/s, NA
VDV

gmayor
12-22-2015, 02:10 AM
This should be fairly straightforward, but you have not supplied sufficient information about your requirement. There appear to be three peak values. Are we looking at the total value of the three when added together, or are we looking at any one of them reaching your threshold, or are we looking at all of them reaching the threshold? Will there always be three items?

Will you be able to identify the messages in a rule, by sender and/or subject?

The following macro can be run from a rule and assumes any of the values can be over the threshold. It includes a a macro for testing with a selected message, before you add it, as a script, to a rule to identify the incoming messages.

Option Explicit
Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.item(1)
PeakExceeded olMsg
lbl_Exit:
Exit Sub
End Sub

Sub PeakExceeded(olItem As MailItem)
Dim sText As String
Dim vText As Variant
Dim vItem As Variant
Dim strSubject As String
Dim i As Long, j As Long
Dim oOutMail As MailItem
With olItem
sText = olItem.Body
vText = Split(sText, Chr(13))
For i = UBound(vText) To 0 Step -1
If InStr(1, vText(i), "Peak") > 0 Then
vItem = Split(vText(i), Chr(32))
strSubject = vText(i - 3)
For j = 1 To UBound(vItem) Step 2
If Val(vItem(j)) > 2 Then
Set oOutMail = CreateItem(olMailItem)
With oOutMail
.To = "someone@somewhere.com" 'The recipient
.Subject = "Alert Level - " & strSubject
.Body = olItem.Body
.sEnd
End With
Exit For
End If
Next j
Exit For
End If
Next i
End With
lbl_Exit:
Exit Sub
End Sub