vba code to send email based on dialog box answer - vba

Here are the steps that I want to happen or something similar. I basically want a dialog box to cancel an email if special instructions were not reviewed:
The email is tagged with a Category named: Special Delivery. This is already done prior to everything.
I complete the job request and ready to send back a reply.
Click on "Reply All"
Email pops up, I can add attachments and body of the text etc...
When I click send I want a dialog box to pop up asking "Did you review delivery instructions?"
If I hit "OK", the email sends, if I hit "Cancel" the email closes and nothing is sent.
This is the code I'm using now; however, it sends the email regardless of what I click... Any help would be great.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Categories = "Special Delivery" And MsgBox("Did you review delivery instructions?", vbOKCancel) <> vbOK Then
Cancel = True
End If
End Sub

Take the Item.Categories out because the Item will not have Categories when your replying and use vbOKCancel) = vbCancel
Example
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim Prompt As String
Prompt = "Did you review delivery instructions?"
If MsgBox(Prompt, vbOKCancel) = vbCancel Then
Cancel = True
End If
End Sub

You can determine which condition is the problem by separating them.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Categories = "Special Delivery" Then
debug.print "Item has Special Delivery category"
If MsgBox("Did you review delivery instructions?", vbOKCancel) <> vbOK Then
Cancel = True
End If
Else
debug.print "If item has Special Delivery category it was not identified"
End If
End Sub

Related

How to check sending email address before sending?

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.

Check the Subject Field in Outlook

I see this code all over the internet and everyone says how wonderful it works but it doesn't for me, I'm clueless, any ideas why?
I have Windows 7 and Outlook 2010.
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
End Sub
Please ensure you have set the correct references in your project for Outlook 2010 Library. Also ensure that Outlook 2010 should already be in running state. Thanks
you posted a link to a screen capture on May 20 at 16:04
go to that same place in the code and delete all the code in the sub, including sub and endsub (the code that you posted)
now click on "application" dropdown at top-left
you should see the following fill in
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
End Sub
if something else pops up, then click on ItemSend in dropdown on top right
put in one command to get this very minimal sub (if it does not work, then nothing else will)
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
msgbox "this works"
End Sub
now send any email, if it works then you should see a dialog box pop up
note: i have found that "just pasting in code" does not always work and you have to click on the dropdown menus, after you paste in the code, to get the sub to be registered

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.

Closing Send Mail Window after Canceling Item Send event within Outlook via VBA

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...

How to make subject field compulsory in Outlook 2007?

I always forget to write subject in email, so I want make the subject field compulsory.
Can you help me please?
Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean)
If Item.Subject = "" Then
Item.Subject = InputBox("Please do not always forget the subject!")
End If
If Item.Subject = "" Then
MsgBox "Won't send this without a subject."
Cancel = True
End If
End Sub
Do While Item.Subject = ""
Item.Subject = InputBox("..")
Loop
I have a similar routine which just checks if I mention the word attachment, and will prompt me if I want to cancel the send, and I have to put in the line:
Item.Display
so that I can go add the attachment. This way you can just prompt with a messagebox saying to add the subject...