outlook 2010 - warning popup before sending email from personal mail - vba

I've outlook 2010 at my work and linked my personal mail id as a primary account and the rest of the group shared mail id are linked at server level.
So when I send a new email from the group, it selects the personal mail id by default and user have to change it everytime.
I use the below macro to provide a warning pop up when the From is selected as my personal id however this macro warn even if the shared mail id is selected in From.
How to prevent this from warning if the From is not the primary id or personal id or is there a macro to automatically select the From when a new email is created based on the group shared mail id's?
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If InStr(LCase(Item.SendUsingAccount), "sara#example.com.") Then
Prompt$ = "You sending this from sara#example.com. 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 Sub

From corresponds to the SentOnBehalfOfName property. This is empty until mail is sent, unless you set it in code before sending. Not useful here as you want to verify if you forgot to set it.
Private Sub SetFrom()
Dim curritem As mailItem
Dim uPrompt As String
Set curritem = CreateItem(olMailItem)
curritem.Display
uPrompt = "This mail has not been sent." & vbCr & vbCr
uPrompt = uPrompt & "The SentOnBehalfOfName (From) property is empty unless set in the code." & vbCr & vbCr
uPrompt = uPrompt & "See between the quotes." & vbCr & vbCr
MsgBox uPrompt & Chr(34) & ActiveInspector.currentItem.SentOnBehalfOfName & Chr(34)
' Note: The From in the user interface does not populate the property."
curritem.SentOnBehalfOfName = "sharedmailbox#example.com"
' For demonstration purposes. Not necessary to display in real code.
curritem.Close olSave
curritem.Display
MsgBox "SentOnBehalfOfName set in the code." & vbCr & vbCr & _
"The SentOnBehalfOfName (From) is set to: " & curritem.SentOnBehalfOfName
ExitRoutine:
Set curritem = Nothing
End Sub
Your attempt to verify SendUsingAccount has likely failed due to having one account with many mailboxes where you want many accounts.
Sub Account_name()
Dim olAcct As account
Dim countAcc As Long
Dim i As Long
countAcc = Session.Accounts.count
For i = 1 To countAcc
Debug.Print "Account.....: " & i
Debug.Print " DisplayName: " & Session.Accounts.Item(i).DisplayName
Debug.Print " UserName : " & Session.Accounts.Item(i).userName
Debug.Print
Next
End Sub
If you find you have only one account see Add an email account to Outlook to to add accounts if you have the required permissions.

Related

Email address in vba not displaying correctly

I have everything working to send an email via an Access command button. However, the displayed email address is incorrect.
Private Sub cmdSendEmail_Click()
Dim EmailApp, NameSpace, EmailSend As Object
Set EmailApp = CreateObject("Outlook.Application")
Set NameSpace = EmailApp.GetNamespace("MAPI")
Set EmailSend = EmailApp.CreateItem(0)
EmailSend.To = [emailadd] '[emailadd] is the field on the form where the button is located
EmailSend.Subject = [Forms]![WorkordersVR]![Project] & " - " & [Forms]![WorkordersVR]![JobNumber]
EmailSend.Body = "Hello," & vbCrLf & vbCrLf & _
"The project" & " " & [Forms]![WorkordersVR]![Project] & " " & "is ready for pickup." & vbCrLf & vbCrLf & _
"Thank you!" & vbCrLf & vbCrLf & _
"Person sending email here" & vbCrLf & _
EmailSend.Display
Set EmailApp = Nothing
Set NameSpace = Nothing
Set EmailSend = Nothing
End Sub
What ends up in the displayed email To is:
"fred#aplace.com#fred#aplace.com#"
How do I get fred#aplace.com?
You can use string functions available in VBA to get a substring until the # symbol in the string. For example, the InStr function returns a number specifying the position of the first occurrence of one string within another.
Also I'd suggest using the Recipients property of the MailItem class which returns a Recipients collection that represents all the recipients for the Outlook item. Then I'd suggest using the Recipient.Resolve method which attempts to resolve a Recipient object against the Address Book.
For example:
Sub CreateStatusReportToBoss()
Dim myItem As Outlook.MailItem
Dim myRecipient As Outlook.Recipient
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Add("email")
myRecipient.Resolve
If(myRecipient.Resolved) Then
myItem.Subject = "Status Report"
myItem.Display
End If
End Sub

Email recipient name verification [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
There is a tool in outlook that notifies a sender about possible missing attachments as they click the send button. This tool seems to search the body of the email for keywords like "attachment" and then check to see if anything is attached to the email. If not, the notification then pops up.
I'm looking for something similar but a bit more advanced. I would like a similar notification pop-up to appear when the body of my email does not contain neither the first name nor the last name of my recipient(s).
EDIT / UPDATE
I have eventually created my own VBA code to solve this problem, based on FaneDuru's answer. Please consider unclosing this question.
Note: my code searches for either the first, middle(s) or last name of each one of the recipients, but only on the first two lines of the body of the email.
If any one of these searches is successful (i.e. if the name is found on the first 2 lines), then the check is successful and the email can be sent, otherwise the sender is notified.
When the recipient's email address is not in the address book of the sender, the programme does other similar things that can be easily seen in the code.
Please feel free to suggest improvements.
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) = "MailItem" Then
Dim myMail As MailItem, recip As Recipient, strNoRef As String, msg As VbMsgBoxResult, msge As VbMsgBoxResult, noEntry As String
Dim i As Integer
Dim j As Integer
Dim Lines() As String
Dim fLines As String
i = 0
j = 0
Set myMail = Item
Lines = Split(myMail.Body, vbCrLf, 4)
fLines = Lines(0) & Lines(1) & Lines(2)
For Each recip In myMail.Recipients
If recip.Address <> recip.AddressEntry Then
i = i + 1
If Not NameExists(recip.AddressEntry, fLines) Then
j = j + 1
strNoRef = strNoRef & recip.AddressEntry & vbCrLf
End If
End If
Next
For Each recip In myMail.Recipients
If Not recip.Address <> recip.AddressEntry Then
noEntry = noEntry & recip.AddressEntry & vbCrLf
End If
Next
If j = i And noEntry = "" Then
msg = MsgBox("This mail does not contain a reference to anyone of the following:" & vbCrLf & _
vbCrLf & strNoRef & vbCrLf & _
"To send the mail anyway, please press ""Yes"".", vbYesNo, "Send the mail?")
If msg <> vbYes Then Cancel = True
End If
If j = i And noEntry <> "" Then
msg = MsgBox("This mail does not contain a reference to anyone of the following:" & vbCrLf & _
vbCrLf & strNoRef & vbCrLf & _
"And the following recipients are not in the address book:" & vbCrLf & _
vbCrLf & noEntry & vbCrLf & _
"To send the mail anyway, please press ""Yes"".", vbYesNo, "Send the mail?")
If msg <> vbYes Then Cancel = True
End If
If noEntry <> "" And j < i Then
msge = MsgBox("The following recipients are not in the address book:" & vbCrLf & _
vbCrLf & noEntry & vbCrLf & "So, the email was not sent." & vbCrLf & _
"To send it, please press ""Yes"".", vbYesNo, "Send the mail?")
If msge <> vbYes Then Cancel = True
End If
If noEntry = "" And j < i Then
Cancel = False
End If
End If
End Sub
Function NameExists(strName As String, strBody As String) As Boolean
Dim arrName, El
arrName = Split(strName, " ")
For Each El In arrName
If InStr(1, strBody, El, vbBinaryCompare) > 0 Then
NameExists = True: Exit Function
End If
Next El
End Function
In order to achieve what you try explaining, please proceed in the next way:
Change Outlook security settings to make it open with Macro Enabled:
File - Options - Trust Center - Trust Center Settings... - Macro Settings and choose Notifications for all Macros, or Enable All Macros (not recommended.... Press 'OK', of course...
Close and reopen Outlook, choosing Enable Macros!
Press F11 in order to access VBE (Visual Basic for Applications) window. In its left pane you will see Project1 (VBAProject.OTM).
Expand Microsoft Outlook Objects and double click on ThisOutlookSession.
In the opening window (to the right side), please copy the next code:
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) = "MailItem" Then
Dim myMail As MailItem, recip As Recipient, strNoRef As String, msg As VbMsgBoxResult, noEntry As String
Set myMail = Item 'just to benefit of intellisense suggestions...
For Each recip In myMail.Recipients 'iterate between mail recipients
If recip.Address <> recip.AddressEntry Then 'if the address has a name (not only xxx#domain.com):
If Not NameExists(recip.AddressEntry, myMail.Body) Then 'check if one of its names (first or last) exists
strNoRef = strNoRef & recip.AddressEntry & vbCrLf 'if not, build a string to be used in the message
End If
Else
noEntry = noEntry & recip.AddressEntry & vbCrLf
End If
Next
If noEntry <> "" Then
MsgBox "The following recipients are not in the address book:" & vbCrLf & _
vbCrLf & noEntry
End If
If strNoRef <> "" Then
msg = MsgBox("The mail you try sending does not contain a reference to" & vbCrLf & _
vbCrLf & strNoRef & vbCrLf & "and it cannot be sent..." & vbCrLf & _
"To send it as it is, please press ""Yes"".", vbYesNo, "Send the mail?")
If msg <> vbYes Then Cancel = True 'if not pressing "Yes", the sending will be cancelled
End If
End If
End Sub
Function NameExists(strName As String, strBody As String) As Boolean
Dim arrName, El
arrName = Split(strName, " ")
For Each El In arrName
If InStr(1, strBody, El, vbBinaryCompare) > 0 Then
NameExists = True: Exit Function
End If
Next El
End Function
It would be good to press Save on the VBE Standard toolbar. I thing Ctrl + S will also work...
Try playing with mails and send some feedback...

Copy Categories for Reply/Reply All and Forward

The common solution on the net is to make a registry edit for all Categories to be transferred from the incoming message to the Reply/Reply all and forward messages.
My office will not allow a registry change.
How can I accomplish the task to COPY and PASTE all of my assigned categories?
It is not usual to categorize outgoing mail. I believe most (maybe all) will find categories are removed upon sending.
Private Sub Categories_reply()
Dim currItem As mailItem
Dim repItem As mailItem
Set currItem = ActiveInspector.currentItem
Set repItem = currItem.reply
repItem.Categories = currItem.Categories
repItem.Display
repItem.Save
If repItem.Categories <> "" Then
MsgBox " Viewed in Drafts folder, with a categories column, you should see: " & _
vbCr & repItem.Categories & vbCr & vbCr & _
"The categories will likely be removed upon sending."
End If
End Sub

Outlook VBA to verify recipient

What is the best way to check for multiple black-list email addresses before sending?
I have several email addresses I am not allowed to send information to as part of a project. I want Outlook to check for any of the black-list email addresses and notify me if they are included before sending. Below is the code I found to modify
For example, my black-list includes: "bad#address.com", "worst#address.com", "evil#address.com"
What is the best way to put these addresses into the code below, would be good for it to be in a way that allows for changing the addresses in the black-list easily?
So here is the latest version of my code with your suggestions. Unfortunately it lets me send the emails to the addresses on the Checklist. Any suggestions?
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
On Error Resume Next
' use lower case for the address
' LCase converts all addresses in the To field to lower case
Checklist = "bad#address.com" & _
"worst#address.com" & _
"evil#address.com" '// , _ and so on
Set Recipients = Item.Recipients
For i = Recipients.Count To 1 Step -1
Set recip = Recipients.Item(i)
If InStr(LCase(recip), LCase(Checklist)) Then
prompt$ = "You sending this to this to " & Item.To & ". Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If
Next i
End Sub
Create a procedure level variable CheckList which is the list of csv of black listed emails. you can initialise this in the procedure as a hard assignment or dynamically retrieve from other data sources for e.g. sql server
Dim lbadFound As Boolean
dim badAddresses as string
lbadFound = False
CheckList = "bad#address.com," & _
"worst#address.com," & _
"evil#address.com" '// , _ and so on
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
lbadFound = true
badAddresses = badAddresses & recip & & vbcrlf
End If
Next i
If lbadFound Then
prompt$ = "You sending this mail to one or more black listed email address(es)" & badAddresses & vbcrlf & " Are you sure you want to send it?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End If

VBA Outlook Script

I have a script I've pieced together over time. Recently a plugin we need to use in Outlook has caused some issues. Basically we get prompted twice because when the plugin is used the e-mail ends up being destroyed and re-created with a specific attachment filename. At this point the user is prompted again. I've tried to work in a For loop to skip the script if it finds this attachment. However, when I added the For loop it just seems to skip the entire script. I have limited experience with VBA and so I'm sure it's an issue with my syntax or usage. See script below:
Private Sub Application_ItemSend _
(ByVal Item As Object, Cancel As Boolean)
Dim strMsg As String
Dim Atmt As Variant
'strMsg = Item.Class
If Item.Class = "43" Then
For Each Atmt In Item.Attachments
If VBA.Right(Atmt.FileName, 3) = ".sf" Then
GoTo NonEmailError
End If
Next Atmt
If Item.CC = "" Then
strMsg = "To recipients: " & Item.To & vbCrLf & _
"Are you sure you want to send this message?"
Else
strMsg = "To recipients: " & Item.To & vbCrLf & _
"Cc recipients: " & Item.CC & vbCrLf & _
"Bcc recipients: " & Item.BCC & vbCrLf & _
"Are you sure you want to send this message?"
End If
Else
GoTo NonEmailError
End If
' Exit Sub
' Ignore errors for now.
On Error GoTo NonEmailError
' Prompt user to fill in subject
If Item.Subject = "" Then
MsgBox "You must enter a subject.", 48, "Empty Subject"
Cancel = True
GoTo NonEmailError
End If
' Exit Sub
' Prompt user to verify E-Mails
If MsgBox(strMsg, vbYesNo + vbQuestion _
, "Send Confirmation") = vbNo Then
Cancel = True
End If
Exit Sub
NonEmailError:
' The item being sent was not an e-mail and so don't prompt the user anything
Exit Sub
End Sub
I am not that familiar with outlook and VBA, but my first guess in this case is that item.class should be compared to the numeric value 43 rather than the string-literal value "43".
The one thing that gives me pause is that I would expect the statement to throw an error because "43" is the wrong type for Item.Class, but this may just reflect my unfamiliarity with the material.
According to msdn reference, item.class (in this case a mailItem) is an OlObjectClass constant, and 43 is olMail. It would probably be advisable to change:
If Item.Class = "43" Then
to
If Item.Class = olMail Then
or at least to:
If Item.Class = 43 Then
( see: http://msdn.microsoft.com/en-us/library/bb208118%28v=office.12%29.aspx and http://msdn.microsoft.com/en-us/library/bb207137%28v=office.12%29.aspx )