I want to check for a specific word in the body of the email.
Then if the To or CC fields do not contain a specific email address present a popup asking whether to send without the address.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String
Dim iIndex As Long
On Error Resume Next
Checklist = "address@example.com"
iIndex = InStr(Item.Body, "word")
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If iIndex > 0 Then
If Not InStr(LCase(recip), LCase(Checklist)) Then
prompt$ = "You may have forgotten to send this to address. Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
End If
Next i
End Sub
Here's my code after a few changes, but still not fully functional:
Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean)
Dim Recipients As Outlook.Recipients
Dim recip As Outlook.Recipient
Dim i
Dim prompt As String
Dim iIndex As Long
Dim addFound As Boolean
Dim address As String
addFound = False
On Error Resume Next
CheckList = "address@example.com"
iIndex = InStr(Item.Body, "word")
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If InStr(1, LCase(CheckList), LCase(recip)) >= 1 Then
addFound = True
address = address & recip & & vbCrLf
Exit For
End If
If iIndex > 0 Then
If addFound = False Then
prompt$ = "You may have forgotten to send this to address. Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
End If
Next i
End Sub
I'm getting an error from address = address & recip & & vbCrLf
of "Compile error: Syntax error" Not sure how to proceed.