Popup will show in the outlook for unwanted recipient - vba

I have a list of 150 people and I don't want to send any email apart from the list. Is it possible to set a macro in the outlook where if I try to send any email to the email id which is not present in the list, I will get a popup before sending the email ?

Yes, it is. You can handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem , is used in a program.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " &; Item.Subject &; "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
Instead of simply displaying a message box asking users permissions to send an Outlook item you may check the Recipients collection (see the corresponding property of the Outlook item). It represents all the recipients for the Outlook item. So, you may check them and compare with your list.

Related

Is there a simple way to reply with sender's name in Outlook?

Outlook is something I use at my job countlessly each day. I was wondering if there is a way that when I click "Reply" or "Reply All" on a message, the email will input "Hello 'Sender's First Name'," automatically so that I'm able to reply with the context of a message. Although this is tedious, it could save lots of time in the long run. I've seen outdated articles on this, along with KuTools for Outlook, but I do not think it is doable. Any help is appreciated!
You can develop a VBA macro or add-in where you could handle the Reply and ReplyAll events.
The Reply event is fired when the user selects the Reply action for an item, or when the Reply method is called for the item. The new item being sent in response to the original message is passed as a parameter, so you can set it up according to your needs. For example:
Public WithEvents myItem As MailItem
Sub Initialize_Handler()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myItem_Reply(ByVal Response As Object, Cancel As Boolean)
Set Response.Body = "Hello dear!"
End Sub
The ReplyAll event is fired when the user selects the ReplyAll action for an item, or when the ReplyAll method is called for the item.

How to forward email after sending?

I use Application_ItemSend to trigger a userform with some inputs, then I want to forward the email using some of those inputs.
What I'm currently doing works if I send a new email rather than forward the original. I assume the original email is not sent until the ItemSend macro finishes and I can't forward an email that hasn't been sent yet.
How can I write a macro that will run after the email is sent?
You are on the right avenue - you need to wait until the item is sent out. Typically Outlook puts sent items to the Sent Items folder, so you can hook up the ItemAdd event on the Sent Items folder and forward the original sent email.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
myOlMItem = Item.Forward()
myOlMItem.Recipients.Add "Eugene Astafiev"
myOlMItem.Send
End Sub
Note, users or other add-ins (VBA macros) can set up a custom folder for keeping sent items in Outlook. In that case you need to check out the MailItem.SaveSentMessageFolder property which returns or sets a Folder object that represents the folder in which a copy of the email message will be saved after being sent. So, you need to set up a hook on that folder too. You can do that in the ItemSend event handler.

Add attachment based on subject of generated e-mail

I have an application which generates e-mails with invoices attached as pdf. The generated e-mails are displayed in Outlook so you can edit them manually before sending.
On all invoices I want to add an extra attachment with a kind of disclaimer. I created a macro and a button which does this. Works fine, but I want this done automaticly upon creation of the e-mail.
My questions:
Does anyone know if it is possible to run a VBA Marco as soon as an e-mail is opened in the editor?
I noticed there is an ItemLoad Event but when this is excecuted, no values has been assigned yet so I cannot check the subject to see if it is an e-mail with an invoice attached.
In ThisOutlookSession
Private WithEvents InspGenSubj As Outlook.Inspectors
Private Sub Application_Startup()
Set InspGenSubj = Inspectors
End Sub
Private Sub InspGenSubj_NewInspector(ByVal Inspector As Inspector)
Dim curritem As Object
Set curritem = Inspector.currentItem
If curritem.Class = olMail Then
If curritem.Sent = False Then
If curritem.Subject = "Generated Mail subject" Then
MsgBox "Code to add attachment to " & curritem.Subject
End If
End If
End If
End Sub
Without knowing how the mail was generated I saved unsent mail to Drafts, closed then reopened.

VBA Macro - customize Reply Button

I've written a macro to add BCC address on reply window. But I want to do the same on click of 'Reply' Button. I can not add macro to this button as it is not custom button. How should I do this?
You can repurpose built-in controls. But in that case you need to develop an add-in, not a VBA macro. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.
Also you may try to handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program. In the event handler you may try to add a new entry to the Recipients collection (see the corresponding property) with the 'Type' property set to olBcc.
This is from superuser.
https://superuser.com/questions/327614/outlook-macro-to-interrupt-a-reply-all
"You can add an event handler via VBA to pick up the ReplyAll event. Something like the following:"
Dim WithEvents insp As Outlook.Inspectors
Dim WithEvents mailItem As Outlook.MailItem
' This is called on Outlook startup
Private Sub Application_Startup()
Set insp = Application.Inspectors
End Sub
' This is called when a new Inspector is created.
' You use it to pick up on a new mail item event
Private Sub insp_NewInspector(ByVal Inspector As Inspector)
' Edit: The size test appears to be incorrect
'If Inspector.CurrentItem.Size = 0 And Inspector.CurrentItem.Class = olMail Then
If Inspector.CurrentItem.Class = olMail Then
Set mailItem = Inspector.CurrentItem
End If
End Sub
' Called when you press ReplyAll
Private Sub mailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)
Dim msg As String
Dim result As Integer
msg = "Do you really want to reply to all?"
result = MsgBox(msg, vbYesNo, "Reply All Check")
If result = vbNo Then
Cancel = True
End If
End Sub
Put the code in the ThisOutlookSession module then restart.

Prompt for password when sending

I want Outlook to prompt for a password or some sort of authentication on all outgoing mail items, because someone keeps sending on behalf on my account.
I have written:
If Omail.SendUsingAccount = "My Domain Email account typed here" Then
Sub password()
Dim pass As String
pass = InputBox("Enter Password")
If pass <> "thepassword" Then Exit Sub
End Sub
This doesn't work. After I have the correct code can I then just insert that into a custom action rule?
Please use the below code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
prompt$ = "Enter Password to Send Mail"
Dim pass As String
pass = InputBox("Enter Password")
If pass <> "yourpwd" Then
Cancel = True
End If
End Sub
Its tested and its working fine.
make sure you have enabled macro from trust centre.
You can develop a VBA macro where you can handle the ItemSend event of the Application class which is fired whenever an Microsoft Outlook item is sent, either by the user through an Inspector (before the inspector is closed, but after the user clicks the Send button) or when the Send method for an Outlook item, such as MailItem, is used in a program.
For example:
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = Outlook.Application
End Sub
Private Sub myOlApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
You may find the Getting Started with VBA in Outlook 2010 article helpful.