VBA Macro - customize Reply Button - vba

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.

Related

Open “Add reminder” dialog for mail item by Close event

I would like to get the dialog box for a follow-up flag and CategoriesDialog box to set a reminder when I close the mailitem.
I tried to modify the code from here. When I close a mailitem, all things remain normal and I get the category dialog. I can not get the dialog box for follow-up flag like this. There is no error message popup.
Public WithEvents objInspector As Outlook.Inspector
Public WithEvents colInspectors As Outlook.Inspectors
Private Sub Application_Startup()
Init_colInspectorsEvent
End Sub
Private Sub Application_ItemLoad(ByVal Item As Object)
Init_colInspectorsEvent
End Sub
Private Sub Init_colInspectorsEvent()
'Initialize the inspectors events handler
Set colInspectors = Outlook.Inspectors
End Sub
Private Sub colInspectors_NewInspector(ByVal NewInspector As Inspector)
If NewInspector.CurrentItem.Class = olMail Then MsgBox "New mail inspector is opened"
If NewInspector.CurrentItem.Class = olTask Then MsgBox "New Task inspector is opened"
If NewInspector.CurrentItem.Class = olContact Then MsgBox "New Contact inspector is opened"
Set objInspector = NewInspector
End Sub
Private Sub objInspector_Close()
If objInspector.CurrentItem.Class = olMail Then 'MsgBox "Mail inspector is closing"
objInspector.CurrentItem.ShowCategoriesDialog
objInspector.CommandBars.ExecuteMso ("AddReminder") 'No error but not work
objInspector.CurrentItem.Save
End If
End Sub
The Outlook object model doesn't provide any property or method for displaying the Add Reminder... dialog.
The best what you can do is to execute a built-in control programmatically:
CommandBars.ExecuteMso ("AddReminder")
But I don't think the Close event handler is the right place for such things.

Popup will show in the outlook for unwanted recipient

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.

How can I create a warning message in Outlook when attaching certain file types?

I have a tendency to, out of habit, send firmware files over email and new policy demands we do otherwise.
How can I generate a pop-up message in Outlook to remind me to place these files on the network rather than sending them through email, based on file extension (.S usually)?
The challenge here is getting the handle of the new item (untitled email) while using the Outlook UI rather than creating a new item via VBA. You need to first set the inspectors collection to a user-defined object, which will eventually contain the parent (new inspector) of our new item (untitled email). That can be done by an application level event such as Startup.
Then, we can use a NewInspector event to see if the new inspector contains a new message or not; if so, we set it to a module level MailItem object that we have defined on top.
Now, we are set to use a BeforeAttachment event to check the extension of the file that is being attached, if the extension is a banned extension, it will prompt a message and will cancel attaching.
You can still improve this by making extension comparison better and more accurate or copying the file with banned extensions to the location you want without making you to do that manually or even opening the folder you need to place the files there using windows explorer.
to place the code: ALT + F11, double click on ThisOutlookSession, paste the code and CTRL + S to save
I hope this helped! :)
Option Explicit
Dim WithEvents myItem As Outlook.MailItem
Private WithEvents myOlInspectors As Outlook.Inspectors
Private Sub Application_Startup()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
Dim msg As Outlook.MailItem
If Inspector.CurrentItem.Class = olMail Then
Set msg = Inspector.CurrentItem
If msg.Size = 0 Then
'MsgBox "New message" 'uncomment to test this routine
Set myItem = msg
End If
End If
End Sub
Private Sub myItem_BeforeAttachmentAdd(ByVal myAttachment As Attachment, Cancel As Boolean)
Dim sExtension As String
Dim sBannedExtension As String
Dim arr As Variant
sBannedExtension = "xlsx,frm,docx,jpg,png"
arr = Split(myAttachment.FileName, ".")
sExtension = arr(UBound(arr))
If InStr(UCase(sBannedExtension), UCase(sExtension)) > 0 Then
MsgBox "Sorry, you cannot send a file with a(n)" & sExtension & " extension as an attachment according to the new policy."
Cancel = True
End If
End Sub

Autofill BCC address

I want to autofill the BCC field with a specific address on replies, forwards and new emails.
I have seen a similar function that performs "silently" - i.e., the BCC address is added once the 'Send' button has been pressed.
I want to be able to remove/change the address if necessary.
From the user's perspective: click reply/forward/new email, and the message window opens up with the BCC field filled.
My knowledge of VBA is somewhat limited, so I'd appreciate if you could be specific about where to place the code.
This code put in ThisOutlookSession module will fire when you click on the button New Email and when replying to an email. From there it's simple to insert whatever you need in the various fields. You need to restart Outlook or manually call Application_Startup() to have it activated the first time.
Option Explicit
Public WithEvents myInspectors As Outlook.Inspectors
Public WithEvents myExplorer As Outlook.Explorer
Private Sub Application_Startup()
Set myInspectors = Application.Inspectors
Set myExplorer = Application.ActiveExplorer
End Sub
Private Sub myInspectors_NewInspector(ByVal Inspector As Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
'MsgBox "new mail"
Inspector.CurrentItem.BCC = "joe.doe#domain.com"
End If
End Sub
Private Sub myExplorer_InlineResponse(ByVal Item As Object)
'MsgBox "reply"
Item.BCC = "jane.dane#domain.com"
End Sub

How to automatically run a macro on opening a draft in Outlook? (add a BCC)

I'm trying to automatically achieve this workflow:
when user opens a message draft in Outlook (a generated EML file)
if the subject matches a string (immutable, known beforehand, I can't change it; it's something like xyžřy, note the non-ASCII characters):
then add an e-mail to BCC field (immutable, known beforehand, valid e-mail address; let's say it's baz#example.com)
I already know the last part - how to add a BCC to a message, and I use InStr for matching:
Sub addbcc()
Dim objRecip As Recipient
Set oMsg = Application.ActiveInspector.CurrentItem
With oMsg
If InStr(1, oMsg.Subject, "xyžřy") > 0 Then
Set objRecip = oMsg.Recipients.Add("baz#example.com")
objRecip.Type = olBCC
objRecip.Resolve
End If
End With
Set oMsg = Nothing
End Sub
However, the user still needs to remember to press a button to run this macro, which is not more convenient than typing the BCC manually. Is it possible to run the macro automatically when this e-mail is opened?
Is it possible to run the macro automatically when this e-mail is opened?
Work with NewInspector Event , Events occurs when new window is opened by user or through your code.
Example
Option Explicit
Private WithEvents Inspectors As Outlook.Inspectors
Private Sub Application_Startup()
Initialize_handler
End Sub
Public Sub Initialize_handler()
Set Inspectors = Application.Inspectors
End Sub
Private Sub Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If Inspector.currentItem.Class = olMail Then
If Inspector.currentItem.Parent = "Drafts" Then ' Drafts Folder
Debug.Print Inspector.currentItem.Subject ' Immediate Window
' Call Your Code
' Inspector.currentItem.BCC = "baz#example.com"
End If
End If
End Sub
CurrentItem Property
You could monitor the drafts folder with ItemAdd. See the idea here for the inbox. How do I trigger a macro to run after a new mail is received in Outlook?
You could add the bcc in ItemSend. Outlook 2010 - VBA - Set bcc in ItemSend