PDA

View Full Version : Message box if sentonbehalfof and contains string



BenC1985
02-21-2017, 02:49 AM
Hey guys, wondering if anybody can provide any help with an Outlook VBA task that I have.


I'm using Outlook 2007 but need the code to work on 2010 also. What I want to acheive is code that runs when an email is sent. If it is sent from a specific shared mailbox I want to check the body of the email for a particular sentence. If it is true, send the item, if it is not true, I want to prompt the user with a msgbox saying "Are you sure you want to send it from this email address?"


To give you an example of the code I have, I'll post it below. The issue with this code is that it displays the prompt no matter what the .sentOnBehalfOf Email Address is. I have this code pasted in 'ThisOutlookSession'.


Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.SentOnBehalfOfName = "EmailAddress" Then
Else
If InStr(Item.Body, "Text to find in email body") > 0 Then
Else
If MsgBox("Are you sure you want to sent this email from EmailAddress", vbOKCancel) <> vbOK Then
Cancel = True
End If
End If
End If
End Sub

gmayor
02-21-2017, 05:07 AM
What I believe you require is to get rid of the Else statements.


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.SentOnBehalfOfName = "EmailAddress" Then
If InStr(Item.Body, "Text to find in email body") > 0 Then
If MsgBox("Are you sure you want to sent this email from EmailAddress", _
vbOKCancel) <> vbOK Then
Cancel = True
End If
End If
End If
End Sub

BenC1985
02-21-2017, 05:38 AM
By removing the Else Statements, the code seems to do nothing at all.

It seems to be the If Statement looking at .SentOnBehalfOf. Is his property also called anything else?

Paul_Hossler
02-21-2017, 09:44 AM
Not tested




Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

If Item.SentOnBehalfOfName = "EmailAddress" Then ' Shared Mailbox

If InStr(Item.Body, "Text to find in email body") = 0 Then ' Text NOT found

If MsgBox("Are you sure you want to sent this email from EmailAddress", vbOKCancel) <> vbOK Then ' Send anyway?
Cancel = True ' User said [Cancel]
Else
Cancel = False ' User said [OK]
End If

Else
Cancel = False ' text must be in there somewhere

End If
End If

End Sub




BTW, I'd use vbYesNo in the MsgBox

skatonni
02-21-2017, 10:57 AM
If you find there is no SentOnBehalfOfName until the mail is sent. You could try making a separate account for the shared mailbox so you can use SendUsingAccount.


Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim sentName As String

sentName = Item.SentOnBehalfOfName

If sentName = "" Then
sentName = "There is no SentOnBehalfOfName yet."
End If

MsgBox "sentName: " & sentName

'Check for the number with "Which_Account_Number"
If Item.SendUsingAccount = Session.Accounts.Item(2) Then

If InStr(Item.body, "Text to find in email body") > 0 Then

If MsgBox("Are you sure you want to sent this email from " & Item.SendUsingAccount, vbOKCancel) <> vbOK Then
Cancel = True
End If

End If

End If

End Sub

Private Sub Which_Account_Number()

Dim i As Long

For i = 1 To Session.Accounts.Count
Debug.Print " Account # " & i & ": " & Session.Accounts.Item(i)
Next i

End Sub