Consulting

Results 1 to 8 of 8

Thread: VBA Code to detect sending an email

  1. #1

    VBA Code to detect sending an email

    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.

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks

  4. #4
    Graham, how do I modify this so that the macro/script runs 10 seconds after sending an email? Thanks.

  5. #5
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    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.
    Last edited by Legaldeejay; 02-27-2021 at 05:46 AM.

  7. #7
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    OK thank you again

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •