I use multiple accounts in Outlook. I want to give a warning box if sending from an address I should not be sending from.
I have two addresses that I should never send from (they are receive only accounts).
This example is almost what I am looking for.
Example - Checking the "To" address.
I believe a string comparison (StrComp) and Item.SenderEmailAddress is what I need.
Here is my attempt for giving a warning for a single email address (bad.email#gmail.com).
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
' use lower case for the address
' LCase converts all addresses in the To field to lower case
If StrComp((Item.SenderEmailAddress), "bad.email#gmail.com") Then
Exit Sub
End If
Prompt$ = "You sending this from " & Item.SenderEmailAddress & ". Are you sure you want to send it?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End Sub
Ideally I would to check two or more addresses with the same code. Something like in the example should work.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Select Case LCase(Item.To)
Case "alias#domain.com", "alias2#domain3.com", "alias3#domain3.com"
Item.Send
Case Else
Prompt$ = "You are not sending this to " & Item.To & ". Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
Cancel = True
End If
End Select
End Sub
Also, where do I place the code to ensure that it is constantly running and ready?
I think it should be MailItem.SendUsingAccount Property (Outlook)
Which returns or sets an Account object that represents the account under which the MailItem is to be sent. Read/write, also see MailItem.SendUsingAccount Property.
Example
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Prompt As String
Prompt = "Are you sure you want to send from 0m3r#Email.com?"
If Item.SendUsingAccount = "0m3r#Email.com" Then
If MsgBox(Prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End If
End Sub
Try using SendUsingAccount - as noted, SenderEmailAddress doesn't exist on unsent mail.
Option Explicit
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim sendAddress As String
Dim prompt As String
' Check Send_from name
sendAddress = Item.SendUsingAccount.SmtpAddress
Select Case sendAddress
Case "alias#domain.com", "alias2#domain3.com", "alias3#domain3.com"
' send
Case Else
prompt = "You are currently sending this email from " & sendAddress & ". Are you sure you want to proceed?"
If MsgBox(prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground + vbDefaultButton2, "Check Address") = vbNo Then
Cancel = True
End If
End Select
End Sub
You should paste this code in ThisOutlookSession, under Microsoft Outlook Objects in the VBA Editor.
Related
I need a pop-up message to remind the person to check all the information before closing the presentation, with two choices (to cancel or to close it anyway).
Private Sub PPTApp_PresentationBeforeClose(Cancel As Boolean)
If MsgBox("Confirmo que as informações desta apresentação estão atualizadas no SAP", _
vbQuestion + vbYesNo) = vbNo Then
Cancel = True
End If
End Sub
Try This.
Private Sub PPTApp_PresentationBeforeClose(Cancel As Boolean)
Dim i As Long
i = MsgBox("your message will appear here", vbQuestion + vbYesNo)
If i = vbNo Then
'do this command
Else
' do this command
End If
End Sub
When I send an Outlook 2016 appointment, I use following to trigger a VBA code:
Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
How to display data specific to the type "AppointmentItem"?
I expect a Message Box "Do you want to talk about Computer at school",
but I get a Run-time error '438'.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim prompt As String
prompt = "Do you want to talk about " & Item.Subject 'Works
prompt = prompt & " at " & Item.Location 'Error
If MsgBox(prompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
If you send a meeting request, the sent item itself is an olMeetingRequest and not an olAppoinmentItem.
You can address the corresponding appointment item by GetAssociatedAppointment
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objAppointment As AppointmentItem
If Item.Class = olMeetingRequest Then
Set objAppointment = Item.GetAssociatedAppointment(False)
Debug.Print objAppointment.Subject
Debug.Print objAppointment.Location
End If
End Sub
Sage Pastel Evolution is unable to send emails in HTML format.
I am trying to intercept email before it is sent, add information to the subject and body in HTML format.
I placed the following in the ThisOutlookSession module:
Option Explicit
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 sPrompt As String
sPrompt = "Are you sure you want to send " & Item.Subject & "?"
If MsgBox(sPrompt, vbYesNo + vbQuestion, "Sample") = vbNo Then
Cancel = True
End If
End Sub
Nothing seems to be happening when I send an email, neither through Evolution nor through Outlook.
Remove everything and try just the following code, make sure to restart Outlook before testing
Private Sub Application_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
If you use this format, you have to run Initialize_Handler.
If you wish, instead of running it manually, you could do this when Outlook starts.
In the ThisOutlookSession module.
Private Sub Application_Startup
Initialize_Handler
End Sub
The script below works great but I have to manually run the Initialize_handler routine every time I open Outlook for it to work.
Public WithEvents myOlApp As Outlook.Application
Public Sub Initialize_handler()
Set myOlApp = CreateObject("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
As far as I can see to make this work automatically I should be able to add the below script to ThisOutlookSession. However this doesn't work and I have no idea why.
My macro security is set properly and it is running the code on startup but for some reason it doesn't work until I manually open the VBA editor click into Initialize_handler and press F5.
Private Sub Application_Startup()
Initialize_handler
End Sub
The convoluted method described here https://msdn.microsoft.com/en-us/library/office/ff865076.aspx indicates "The sample code must be placed in a class module".
I suggest you use the special class module ThisOutlookSession only. You could experiment with your own class module but if you just want this to work then replace all your code with this in ThisOutlookSession.
Private Sub Application_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 can instead put it directly in ThisOutlookSession:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "Sent somthing!"
End Sub
As simple as that. Note you need to change your outMail.Display to outMail.Display (True) and there you are, full code:
...
...
...
outMail.Display (True)
On Error Resume Next
bSent = outMail.sent 'This will NOT SEND. Used to get error.
If Err <> 0 Then
'Email was sent. Put followed actions here.
Else
'Email was not sent. Put followed actions here.
End If
On Error GoTo 0
Pros:
You get what you want
It's simple.
Cons:
Excel (or any other runtime you are running this code from) will freeze until you cancel or send email.
I'm coding a VBA Macro to prevent sending emails to a specified email address. It's an Outlook Macro which runs under the ThisOutlookSession. Code runs fine, but the problem is that I can't close the Send Mail window.
I added a line (marked in the code) which throws an error that "The Item.Close command cannot be performed during Item.Send event"
It's understandble, but how can I overcome this?
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.To = "some#domain.com" Then
Prompt$ = "Are you sure you want to send this message?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check before Sending") = vbNo Then
Cancel = True
Item.Close olDiscard ' <<< ERROR HERE
End If
End If
End Sub
Instead of closing the Item itself which can't be done when the send event is still running you close the Item Inspector.
See below:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objInsp As Inspector
Dim Strmsg As String
Set objInsp = Item.GetInspector
If Item.To = "testmail#gmail.com" Then
Strmsg = "Are you sure you want to send this message?"
If MsgBox(Strmsg, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check before Sending") = vbNo Then
Cancel = True
objInsp.Close 1
End If
End If
End Sub
In languages other than VBA, you can use a timer - enable the timer in the ItemSend event, when the timer event fires (you will be out of the ItemSend event handler by then), disable the time and close the inspector.
I don't think you can use a timer in Outlook VBA though...