PDA

View Full Version : VBA Code to detect sending an email



Legaldeejay
02-25-2021, 05:53 AM
Hello. I would like to run a macro/script when sending an email. Is there any VBA Code which will detect when an email is sent in Outlook 365? Thanks.

gmayor
02-25-2021, 06:53 AM
Use the Application_ItemSend event in the ThisOutlookSession module e.g.


Option Explicit


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
With Item
'do stuff e.g.
MsgBox .Subject, vbInformation
End With
End Sub

Legaldeejay
02-25-2021, 08:32 AM
Thanks

Legaldeejay
02-26-2021, 11:07 PM
Graham, how do I modify this so that the macro/script runs 10 seconds after sending an email? Thanks.

gmayor
02-27-2021, 05:22 AM
The macro runs when you click the send button and any delay will occur before the message is sent. You could use events to run the macro when it arrives in the sent mail box, but it won't actually arrive there until the macro is completed, however see below. Run Application startup to initialise the event or restart Outlook.


Option Explicit

Private WithEvents Items As Outlook.Items


#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If


Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
' default local Inbox
Set Items = objNS.GetDefaultFolder(olFolderSentMail).Items
End Sub


Private Sub Items_ItemAdd(ByVal item As Object)
On Error GoTo err_Handler
Sleep 10000
If TypeName(item) = "MailItem" Then
With item
'do stuff e.g.
MsgBox .Subject, vbInformation
End With
End If
lbl_Exit:
Exit Sub
err_Handler:
MsgBox Err.Number & " - " & Err.Description
Err.Clear
GoTo lbl_Exit
End Sub

Legaldeejay
02-27-2021, 05:32 AM
Thank you Graham. But I actually want the macro to run 10 seconds after I click the send button, but before it is actually sent. I already have a rule in Outlook to delay sending email for 1 minute. Can this be done?

Also, I have 2 email accounts, but I only want this VBA Code to work when I send email through my Exchange account.

Thanks.

gmayor
02-28-2021, 02:10 AM
There is no facility to run a macro 10 seconds after clicking Send. You can introduce a pause (see below) but it will still start the macro as soon as you click Send (if that's what triggers it) and the macro will then hang for 10 seconds before completing.


Option Explicit

#If VBA7 Then
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If




Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.SendUsingAccount.DisplayName = "name of account" Then
If TypeName(Item) = "MailItem" Then
On Error GoTo err_Handler
Sleep 10000 'wait ten seconds
If TypeName(Item) = "MailItem" Then
With Item
'do stuff e.g.
MsgBox .Subject, vbInformation
End With
End If
End If
End If
lbl_Exit:
Exit Sub
err_Handler:
MsgBox Err.Number & " - " & Err.Description
Err.Clear
GoTo lbl_Exit
End Sub

Legaldeejay
02-28-2021, 04:40 AM
OK thank you again